8

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?

1 Answer 1

13

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
}
2
  • Works like a charm. Thank you!
    – Lucian
    Jan 8, 2019 at 20:01
  • 1
    But how can you add this value to the db? When I try to add an entry with the FK (for example your idProfile) I get the error Type 'number' has no properties in common with type 'DeepPartial<Promise<Issue>>'
    – TeslaXba
    Jun 3, 2020 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.