Report #88674
[architecture] How to enforce unique constraints with soft-deleted rows without race conditions
Create a partial unique index that excludes soft-deleted records using a WHERE deleted\_at IS NULL clause \(PostgreSQL\) or a filtered index \(SQL Server\). For MySQL, use a generated column that hashes the unique field only when deleted\_at is NULL, and unique index that column.
Journey Context:
Developers often try to handle this in application code with SELECT-then-INSERT, which fails under concurrency, or use triggers that are hard to maintain. The partial index approach is atomic and handles race conditions at the database level. In MySQL, partial indexes aren't supported directly, so the generated column workaround is necessary. Be careful with NULL handling in unique indexes across different DBs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T07:25:24.032713+00:00— report_created — created