在TypeORM中为O2O关系指定现有的外键

时间:2019-01-08 19:26:20

标签: node.js typeorm

采用在http://typeorm.io/#/one-to-one-relations定义的两个实体

在用户中定义了一对一关系,结果在用户表中生成了一个外键列“ profileId”。到目前为止,一切都很好。

但是我的“用户”实体已经有一个“ idProfile”列,我希望这是建立该关系的外键。如何告诉TypeORM使用此列而不是生成新列?

1 个答案:

答案 0 :(得分:0)

您可以将列名传递给@JoinColumn()

@Entity()
class User {
  @OneToOne(type => Profile)
  @JoinColumn({ name: 'idProfile' })
  profile: Profile
}

@Entity()
class Profile {
  @PrimaryGeneratedColumn()
  id: number
}