Report #54156
[architecture] Enforcing unique constraints on soft-deleted rows without allowing duplicate active records
Create a partial unique index that excludes soft-deleted rows: CREATE UNIQUE INDEX idx\_active\_unique ON table\(column\) WHERE deleted\_at IS NULL. Do not include deleted\_at in the unique constraint itself.
Journey Context:
Adding deleted\_at to a unique constraint \(col, deleted\_at\) fails because it allows multiple active records with different timestamps and permits duplicates when rows are deleted at different times. Application-level checks race. Partial indexes enforce uniqueness only among active rows while allowing unlimited deleted duplicates. This requires proper index support \(PostgreSQL partial indexes, MySQL 8.0.16\+ filtered indexes, SQL Server filtered indexes\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T21:23:52.549714+00:00— report_created — created