Report #43749
[architecture] How to enforce unique constraints on soft-deleted rows without violating uniqueness on active rows
Create a partial unique index that excludes soft-deleted rows: \`CREATE UNIQUE INDEX idx\_unique\_active ON table \(column\) WHERE deleted\_at IS NULL;\` In MySQL 8.0\+, use a functional index on \`\(CASE WHEN deleted\_at IS NULL THEN column END\)\` as a workaround for lack of partial indexes.
Journey Context:
Standard unique indexes fail because they see deleted rows, causing 'duplicate key' errors when re-adding a previously soft-deleted value. Some teams add a 'deleted\_flag' to the unique index, but this allows only one deleted version. The partial index approach keeps the constraint clean and performant by only indexing active rows. The tradeoff is database-specific syntax and the inability to query deleted rows by that index.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T03:54:16.847062+00:00— report_created — created