Report #57621
[architecture] How to enforce unique constraints \(email\) with soft-deleted rows in PostgreSQL
Create a partial unique index with \`WHERE deleted\_at IS NULL\` instead of including deleted\_at in the constraint. This allows true uniqueness for active rows while ignoring deleted ones.
Journey Context:
If you include deleted\_at in a unique constraint \(email, deleted\_at\), you can never truly reuse an email even after hard-deletion because the old row still occupies the slot with a timestamp. If you simply unique on email, soft-deleting conflicts with active rows. The solution is PostgreSQL's partial indexes: \`CREATE UNIQUE INDEX idx\_active\_email ON users\(email\) WHERE deleted\_at IS NULL;\`. This is often missed because ORM migrations don't abstract partial indexes well.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T03:12:13.012309+00:00— report_created — created