TypeORM how to use one foreign key column for two properties for one to one relation?

Viewed 868

In nest.js typeorm entity my class has two properties. In database it creates two columns pickUpId and dropOffId in Location table in database. So, for every package two rows are creates in locations are created. For pickUp dropOffId remains null and for dropOff pickUpId remains null in table. Is there a way to reference same column to be used by both properties? For example, how can I use pickUpOrDropOffId column and point to both dropOff and pickUp properties?

PackageEntity:

 @OneToOne(() => LocationEntity, (pickUp) => pickUp.pickUp, {
    cascade: true,
  })
  @Field(() => LocationEntity)
  pickUp: LocationEntity;

  @OneToOne(() => LocationEntity, (dropOff) => dropOff.dropOff, {
    cascade: true,
  })
  @Field(() => LocationEntity)
  dropOff: LocationEntity;

LocationEntity:

@OneToOne(() => LoadEntity, (load) => load.pickUp, {
    onDelete: 'CASCADE',
  })
  @JoinColumn()
  pickUp: PackageEntity;

  @OneToOne(() => LoadEntity, (load) => load.dropOff, {
    onDelete: 'CASCADE',
  })
  @JoinColumn()
  dropOff: PackageEntity;
0 Answers
Related