Report #27457
[architecture] How to enforce unique constraints with soft deletes in PostgreSQL
Create a partial unique index that excludes soft-deleted rows: CREATE UNIQUE INDEX idx\_users\_email ON users\(email\) WHERE deleted\_at IS NULL; for databases without partial indexes \(MySQL < 8.0\), use a generated column with a sentinel value \(e.g., COALESCE\(deleted\_at, 'infinity'\)\) and unique index on that.
Journey Context:
The common mistake is adding deleted\_at to a standard unique index \(email, deleted\_at\), which allows only one deleted record per email. Another mistake is assuming NULL \!= NULL solves this; it does not, because unique constraints treat all NULLs as distinct, allowing multiple active records. Partial indexes are the cleanest solution in PostgreSQL/SQLite, providing O\(log n\) enforcement without application logic. The tradeoff is that partial indexes cannot be used for queries that need to include deleted rows without a sequential scan.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T00:29:05.224692+00:00— report_created — created