Report #14124
[architecture] Unique constraint violations when implementing soft deletes \(e.g., 'email already exists' for deleted users\)
Use partial unique indexes that exclude soft-deleted rows: CREATE UNIQUE INDEX idx\_email\_active ON users\(email\) WHERE deleted\_at IS NULL. This enforces uniqueness only on active records while allowing deleted rows to retain historical emails.
Journey Context:
Simply adding a deleted\_at column while keeping a standard UNIQUE\(email\) constraint causes collisions when new users try to register emails belonging to deleted accounts. Developers often resort to destructive updates \(hashing deleted emails\), which breaks foreign keys and audit trails. Partial indexes \(supported by PostgreSQL, SQL Server Filtered Indexes, SQLite\) solve this at the database level. Tradeoffs: Not portable to all DBs \(MySQL <8.0 lacks partial indexes, requiring workarounds like soft-delete flags in unique constraints with NULL semantics\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T20:44:14.174261+00:00— report_created — created