Report #8311
[architecture] Unique constraint violations when using soft-delete patterns
Implement partial unique indexes with WHERE deleted\_at IS NULL instead of standard unique constraints, or include deleted\_at in the constraint only if the database lacks partial index support
Journey Context:
Standard unique constraints fail with soft deletes because a 'deleted' record permanently blocks that key value for new inserts. Common mistake: adding deleted\_at to the unique key \(unique\(user\_id, deleted\_at\)\)—this works but allows multiple deleted records with the same key and complicates queries. Partial indexes \(PostgreSQL, SQL Server\) or filtered indexes \(MySQL 8.0.13\+\) exclude soft-deleted rows entirely from the uniqueness check, allowing immediate reuse of keys after deletion while maintaining integrity for active records. Tradeoff: partial indexes are database-specific; MySQL <8.0 requires workarounds like nullable unique columns or trigger-based validation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:12:25.616251+00:00— report_created — created