【问题标题】:Many to many relationship in NestJS + TypeORM - custom names for resultant table?NestJS + TypeORM 中的多对多关系 - 结果表的自定义名称?
【发布时间】:2021-10-17 19:45:15
【问题描述】:

我有一个 User 和 Product 表,与:

@Entity('User')
  export class User {
  ...
    @ManyToMany(() => Product)
    @JoinTable({ name: 'UserProduct' })
    Products: Product[];
}

生成的 UserProduct 表中的结果符合预期,有 2 列:userId, productId - 我如何自定义名称以符合我的约定?即 UserId、ProductId

【问题讨论】:

    标签: nestjs typeorm


    【解决方案1】:

    您是否查看过 @JoinTable 的 TypeORM 文档?似乎它们允许您使用 joinColumn 以您的特定列命名要求覆盖标准命名约定。

    我想你想要这样的东西:

    @JoinTable({
        name: "UserProduct",
        joinColumn: {
            name: "UserId",
            referencedColumnName: "id"
        },
        inverseJoinColumn: {
            name: "ProductId",
            referencedColumnName: "id"
        }
    })
    

    【讨论】:

    • 谢谢!我找到了用 JoinColumn 覆盖的方法,但不是用 JoinTable - 这很有效
    猜你喜欢
    • 2022-01-02
    • 2021-12-26
    • 2020-09-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多