【问题标题】:Configure TypeORM default foreign key to follow underscore format instead of camelCase配置 TypeORM 默认外键以遵循下划线格式而不是 camelCase
【发布时间】:2020-10-31 09:43:23
【问题描述】:

有没有办法让生成的所有外键都遵循 下划线 user_id 而不是 camelCase userId

有没有办法配置TypeORM,所以我在定义关系时不必考虑它。

  `userId` varchar(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL

【问题讨论】:

    标签: nestjs typeorm


    【解决方案1】:

    是的,当您像这样定义列时,可以通过指定 name 属性来实现(在此处查看所有可能的选项 https://typeorm.io/#/entities/column-options):

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

    然后数据库字段将为user_id,但在访问实体时,它将被映射回userId

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2019-12-14
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多