Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify existing foreign key for O2O relation in TypeORM

Take the two entities defined at http://typeorm.io/#/one-to-one-relations

A one-to-one relation is defined in User and as a result a foreign key column "profileId" is generated in the User table. So far, so good.

But my "User" entity already has an "idProfile" column and I would like this to be the foreign key on which the relation is built. How can I tell TypeORM to use this column instead of generating a new one?

like image 779
Lucian Avatar asked Jan 08 '19 19:01

Lucian


1 Answers

You can pass the column name to @JoinColumn():

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

@Entity()
class Profile {
  @PrimaryGeneratedColumn()
  id: number
}
like image 166
Niklas Higi Avatar answered Nov 20 '22 20:11

Niklas Higi