Creating Related Rows as One Unit of Work

An operation creating a parent and children can make their changes succeed or fail together when the application promises a complete result.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace an incomplete order
  2. Test the failure after the first write
  3. Simulate failure after inserting the parent
  4. Check the successful pair through the normal read path

Trace an incomplete order

An order insert succeeds and a line insert fails its quantity rule. The application can roll back the entire transaction. A row constraint explains why the second change was rejected.

Test the failure after the first write

After failure, check the order as well as the rejected line. Partial success describes why response status alone cannot establish the final state.[1][2]

Simulate failure after inserting the parent

Creating an order and its first required line can be one transaction. If line validation fails after the order insert, rolling back should leave neither visible outside the transaction. If an empty order has valid meaning, the creation contract may differ. See transaction boundaries.

Begin
Insert order 7
Insert invalid required line → error
Roll back
Another connection should see no new order under this contract

Check the successful pair through the normal read path

Verify both records and the relationship after commit. Test retries when an earlier request may already have persisted them. Keep irreversible external work consistent with the transaction design.

References

  1. PostgreSQL Global Development Group, Data Definition ↩
  2. PostgreSQL Global Development Group, SQL Language Tutorial ↩