Report #58523
[architecture] How to maintain unique constraints \(e.g., email\) with soft-deleted records without blocking reuse forever
Create partial unique indexes that exclude soft-deleted rows: CREATE UNIQUE INDEX idx\_users\_email ON users\(email\) WHERE deleted\_at IS NULL; implement queries to explicitly filter on deleted\_at IS NULL to ensure index usage.
Journey Context:
Standard unique indexes on email prevent re-using a deleted email forever, breaking business logic. Adding a 'deleted' boolean doesn't solve the uniqueness problem. The partial index approach works because PostgreSQL \(and MySQL 8.0.13\+, SQL Server filtered indexes\) allow unique constraints on subsets. Tradeoff: Queries selecting 'all records including deleted' become slow \(full scan\), and the pattern is database-specific. Alternative 'archived' tables avoid this but complicate foreign key cascades.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T04:43:10.210712+00:00— report_created — created