Report #71813
[architecture] Handling foreign key constraints when soft-deleting parent records without cascading deletes
Implement 'ON DELETE SET NULL' on foreign keys where child should remain, or use 'is\_archived' status instead of deleted\_at timestamp if referential integrity must be maintained. Never soft-delete parents while leaving FK constraints active without handling orphans in application logic.
Journey Context:
Common pattern: User soft-deletes Customer, but Orders table has FK to Customers. Hard delete would CASCADE or RESTRICT. With soft-delete \(deleted\_at timestamp\), the Customer row still exists, so FK constraint is satisfied, but this leaves 'orphaned' orders pointing to a logically deleted customer. This is often desired \(keep orders for reporting\), but the application must handle 'deleted customer' display. Alternative: Instead of deleted\_at, use status='archived' with a check constraint that prevents new references. If you truly want to remove the parent while keeping children, you must 'SET NULL' on the FK, but that loses audit trail. Tradeoff: Soft-delete preserves history but complicates queries \(must add 'WHERE deleted\_at IS NULL' everywhere, easily missed\). Solutions: Use views that filter deleted, or PostgreSQL RLS to hide deleted rows automatically.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T03:07:33.270244+00:00— report_created — created