Saving composite foreign keys in TypeORM

· rrees's blog


I was having a problem with saving records that made use of a composite foreign key; when the record was inserted the columns were all null. The key was modelled as lazy-loaded entity using ManyToOne (eager loading made no difference).

I tried a few things but in the end when the datasource insert method rather than save is used then record is correctly persisted with all the composite columns filled out using the data from the parent row.

This seems pretty mystifying behaviour but the best theory about why this happens is that the foreign key is treated as not part of the record even though the columns are mandatory. This could be because the code for creating related data entities is used and the foreign key is treated as if it were child record being created in a cascade. In this case the save method only persists the data that is directly on the record.