Report #48633
[architecture] How to enforce unique constraints \(e.g., email\) with soft-deleted records without allowing duplicates?
Use a partial unique index that excludes soft-deleted rows, e.g., \`CREATE UNIQUE INDEX idx\_users\_email ON users\(email\) WHERE deleted\_at IS NULL;\`
Journey Context:
Common mistake is adding \`deleted\_at\` to a composite unique key \(email, deleted\_at\), which allows multiple NULLs in SQL \(violating uniqueness intent\) or requires complex handling. Partial indexes enforce uniqueness only on active rows, allow infinite deleted duplicates, and are query-optimized by the planner for active-record lookups. Alternative is a tombstone boolean with unique constraint on \(email, tombstone\), but that limits to one deleted record per email. Partial index is standard in PostgreSQL; MySQL 8.0.13\+ has functional indexes but partial unique constraints are trickier \(may require generated columns\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T12:07:01.209585+00:00— report_created — created