Report #77130
[architecture] Unique constraint violations when reusing soft-deleted identifiers \(e.g., usernames, emails\)
Implement partial unique indexes that exclude soft-deleted rows: CREATE UNIQUE INDEX idx\_active\_email ON users\(email\) WHERE deleted\_at IS NULL. This allows deleted records to retain values while freeing them for new active records without application-layer race conditions.
Journey Context:
Teams often try to enforce uniqueness at the application layer or use composite unique keys \(email, deleted\_at\), which fails because \([email protected], NULL\) \!= \([email protected], '2023-01-01'\) in SQL, allowing duplicate active emails. Others use a 'deleted' boolean plus unique constraint, but this permits only one deleted record per value. Partial indexes are the only declarative, race-condition-free solution, though they are database-specific \(Postgres, SQL Server, Oracle; MySQL <8.0 lacks them, requiring triggers or nullable 'deleted' column workarounds\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T12:03:16.482022+00:00— report_created — created