Search code examples
nestjstypeorm

Configure TypeORM default foreign key to follow underscore format instead of camelCase


Is there a way so all foreign key generated follows underscore user_id instead of camelCase userId.

Is there a way to configure TypeORM so I don't have to think about it when define the relation.

  `userId` varchar(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL

Solution

  • Yes this is possible by specifiying name property when you define your column like such (see all possible options here https://typeorm.io/#/entities/column-options):

    @Column('int', { 'name': 'user_id' })
    userId: number
    

    The database field will then be user_id but when accessing the entity it will be mapped back to userId