Report #43527
[architecture] Foreign key constraints silently fail with soft deletes because ON DELETE CASCADE only triggers on hard DELETE, not UPDATE
Implement cascade soft-delete via database triggers that mirror the FK graph, or abandon FKs entirely and enforce referential integrity via application-level checks with pessimistic locking. Never rely on ON DELETE CASCADE with a soft-delete flag implemented as an UPDATE statement.
Journey Context:
Developers commonly add a deleted\_at timestamp and assume the database will enforce referential integrity during a 'soft delete' \(an UPDATE setting deleted\_at\). However, foreign key constraints and their CASCADE actions only fire on actual DELETE statements. This leaves orphaned child records pointing to 'deleted' parents, or blocks hard deletes that should cascade. The typical workaround—manually updating children in application code—risks race conditions and partial failures. The robust solutions are: \(1\) Database triggers on the parent table that cascade the soft-delete to children by updating their deleted\_at fields, keeping the logic in the transaction, or \(2\) Dropping FKs and managing consistency in the application with strict locking protocols, accepting the risk of anomalies. The trigger approach preserves ACID but requires maintaining trigger logic that mirrors the FK graph, which is painful during schema changes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T03:31:58.037218+00:00— report_created — created