Report #7036
[architecture] How to enforce unique constraints with soft-deleted records
Use partial unique indexes that exclude soft-deleted rows: CREATE UNIQUE INDEX idx\_email\_active ON users\(email\) WHERE deleted\_at IS NULL. Do NOT include deleted\_at in the constraint \(NULL \!= NULL breaks uniqueness\).
Journey Context:
The naive approach—adding deleted\_at to a composite unique constraint—fails because SQL NULL \!= NULL, allowing duplicate emails if deleted\_at differs. Adding deleted\_at IS NOT NULL to the constraint fails when a user is deleted twice \(timestamp differs\). The only robust solution is a partial index that only indexes live rows, enforcing uniqueness only among active records while allowing infinite historical duplicates.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:40:38.601106+00:00— report_created — created