Report #14308
[architecture] How to enforce unique constraints \(e.g., email\) with soft-deleted records in PostgreSQL
Use partial unique indexes that exclude soft-deleted rows, e.g., CREATE UNIQUE INDEX idx\_unique\_email ON users\(email\) WHERE deleted\_at IS NULL;
Journey Context:
Standard unique constraints fail because soft-deleted records still occupy the unique slot. Alternatives like hard deletion lose audit history, while status enums require application-level enforcement that race conditions can violate. Partial indexes leverage the database's concurrency control to enforce uniqueness only on active records with zero application overhead.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T21:14:49.831251+00:00— report_created — created