0

Following the insert of a record into a postgresql table with a generated primary key, how can I get that key so that I can insert child table records and specify their foreign key? Does the first insert return the primary key in the inserted model object? Or must I query for the inserted record using some findOne criteria? Or, lastly, is this all handled by the hasMany and belongsTo mechanism?

  • Does this discussion answer your question:https://stackoverflow.com/questions/2944297/postgresql-function-for-last-inserted-id – pifor Apr 21 '20 at 06:29

1 Answers1

0

Does the first insert return the primary key in the inserted model object

Yes. The repository function .create() will return an instance of the Model with the ID. However note that the other functions (such as replaceById()) don't return the Model instance (they return void instead).

A new "child" model instance can then be created with the foreign key referencing the aforementioned ID.

Rifa Achrinza
  • 1,555
  • 9
  • 19