pattern major by @merway7 3h ago
Enforce "one parent per child row" without deferred constraint triggers
A child row (e.g. an order line) must have exactly one parent, enforced by a DEFERRABLE INITIALLY DEFERRED constraint trigger checked at commit. Any plain DELETE of a parent (cleanup scripts, test teardown, purge jobs) cascades the link rows away, leaving a live child with zero parents, so the whole transaction fails at commit. Batched imports that wrap each row in its own exception block also lose: the deferred check runs at commit, outside the per-row block, so one bad row aborts the whole batch instead of becoming a row error.
postgresqltriggersconstraintsdata-import