Skip to content

Saving an entity with a 1:1 cascading insert does not return id if entity has nullable many:one relationship #3246

@danimbrogno-pml

Description

@danimbrogno-pml

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

  • Broker has many Orders
  • Orders have one OrderCustomer
  • The relationship between Order and Broker is not nullable {nullable: false}
  • The relationship between Order and OrderCustomer has cascade insert and update on { cascade: 'update', 'insert']}
  • Use a customer repository to save an Order and OrderCustomer in one call using cascade
  • The save executes with the following query
query: START TRANSACTION
query: INSERT INTO "order"("orderReferenceNumber", "orderCustomerId", "createdDate", "modifiedDate", "companyId") VALUES ($1, DEFAULT, DEFAULT, DEFAULT, $2) RETURNING "id", "createdDate", "modifiedDate" -- PARAMETERS: ["abcd",1]
query: INSERT INTO "order_customer"("name", "createdDate", "modifiedDate") VALUES ($1, DEFAULT, DEFAULT) RETURNING "id", "createdDate", "modifiedDate" -- PARAMETERS: ["Dave"]
query: UPDATE "order" SET "orderCustomerId" = $2, "modifiedDate" = CURRENT_TIMESTAMP WHERE "id" = $1 RETURNING "modifiedDate" -- PARAMETERS: [1,1]
query: COMMIT
  • The data is saved correctly but the object returned back lacks the primary key Order.id
Order {
  orderReferenceNumber: 'abcd',
  orderCustomer:
   OrderCustomer {
     name: 'Dave',
     id: 1,
     createdDate: 2018-12-13T19:20:36.206Z,
     modifiedDate: 2018-12-13T19:20:36.206Z },
  company:
   Broker {
     name: 'Acme Inc.',
     id: 1,
     createdDate: 2018-12-13T19:20:36.178Z,
     modifiedDate: 2018-12-13T19:20:36.178Z },
  orderCustomerId: 1,
  modifiedDate: 2018-12-13T19:20:36.206Z }
  • Change the relationship between Order and Broker to be nullable {nullable: true}
  • Rerun the save
  • Save now executes with a different query
query: START TRANSACTION
query: INSERT INTO "order_customer"("name", "createdDate", "modifiedDate") VALUES ($1, DEFAULT, DEFAULT) RETURNING "id", "createdDate", "modifiedDate" -- PARAMETERS: ["Dave"]
query: INSERT INTO "order"("orderReferenceNumber", "orderCustomerId", "createdDate", "modifiedDate", "companyId") VALUES ($1, $2, DEFAULT, DEFAULT, $3) RETURNING "id", "createdDate", "modifiedDate" -- PARAMETERS: ["abcd",1,1]
query: COMMIT
  • The data is saved correctly and the object returned back now has the primary key Order.id
Order {
  orderReferenceNumber: 'abcd',
  orderCustomer:
   OrderCustomer {
     name: 'Dave',
     id: 1,
     createdDate: 2018-12-13T19:22:56.624Z,
     modifiedDate: 2018-12-13T19:22:56.624Z },
  company:
   Broker {
     name: 'Acme Inc.',
     id: 1,
     createdDate: 2018-12-13T19:22:56.603Z,
     modifiedDate: 2018-12-13T19:22:56.603Z },
  orderCustomerId: 1,
  id: 1, // <= Id is now present
  createdDate: 2018-12-13T19:22:56.624Z,
  modifiedDate: 2018-12-13T19:22:56.624Z }

I will submit a pull request with a minimal reproduction.

Thanks for the great work!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions