Report #73469
[architecture] Soft-delete breaks unique constraints and foreign key referential integrity in SQL databases
Implement partial unique indexes \(PostgreSQL: CREATE UNIQUE INDEX ... WHERE deleted\_at IS NULL\) to allow re-creation of deleted records while maintaining uniqueness for active rows. For foreign keys, either cascade soft-delete to children via application logic or use PostgreSQL's deferrable constraints \(DEFERRABLE INITIALLY DEFERRED\) if immediate enforcement isn't required.
Journey Context:
Simply adding deleted\_at violates unique constraints meant for natural keys \(e.g., email\), preventing a user from re-registering after deletion. Partial indexes solve this by excluding soft-deleted rows from uniqueness checks. For FKs, soft-deleting a parent while hard-deleting children creates orphans; instead, soft-delete children or use ON DELETE RESTRICT and handle the cascade in application code, using deferrable constraints to avoid ordering violations during transactions.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T05:54:38.436511+00:00— report_created — created