How do I keep a unique constraint on a column like email while using soft deletes?
Question
I'm using SoftDeletes on the users table in Laravel. A user can be soft-deleted and later wants to register again with the same email. But the unique index on `email` rejects the new record because the old (soft-deleted) row is still in the table. How do I keep email uniqueness only among active users, without counting deleted rows? I'm on PostgreSQL, but I'm curious about the MySQL side too.
Answer
Short answer
The problem is that a plain unique index folds soft-deleted rows into the uniqueness check. The fix depends on your database: a partial (conditional) unique index in PostgreSQL, a generated column in MySQL.
Context
Either way, keep the DB constraint and add app-level validation on top.
What to do
-
PostgreSQL: a partial unique index is the cleanest path. Enforce uniqueness only among live rows:
CREATE UNIQUE INDEX users_email_active_unique ON users (email) WHERE deleted_at IS NULL;Deleted rows don’t enter the index; re-registering with the same email is allowed, while two active records are blocked. In a Laravel migration you add this as raw SQL via
DB::statement(...), because$table->unique()won’t produce a WHERE. -
MySQL has a trap: NULLs count as “distinct” in a unique index. So
UNIQUE(email, deleted_at)won’t work — two active rows both havedeleted_atNULL, MySQL treats them as different, and it lets duplicate actives through. The correct fix is a generated column:ALTER TABLE users ADD email_active VARCHAR(255) GENERATED ALWAYS AS (IF(deleted_at IS NULL, email, NULL)) STORED, ADD UNIQUE (email_active);An active row carries
email(enforced unique); a deleted row becomes NULL (which doesn’t clash, since NULLs count as distinct). In Laravel you declare it with->storedAs(). -
A simple but data-mutating alternative: mangle the email on delete. On soft-delete, rewrite
emailto something likebora+deleted-42@...; the address frees up and the old row keeps a unique-but-mangled value. This works on any DB, but you lose the original email — store it in a separate column if you need it back. -
Add app validation too — but not instead of the DB. In Laravel, validate against active rows:
Rule::unique('users', 'email')->whereNull('deleted_at')Validation gives the user a clean message, but on its own it’s open to a race condition. The real guarantee is the DB constraint; validation is the UX layer.
-
Think through the restore scenario. If you want to restore a deleted user but a new active record was created with the same email in the meantime,
restore()will hit the constraint. Catch that in code and handle it as “this email is now in use”.
Bottom line: personally I’d set up the partial index on deleted_at IS NULL plus Rule::unique()->whereNull('deleted_at') on PostgreSQL — correct and readable. On MySQL I’d prefer the generated column, and if I didn’t need to preserve the original email I’d consider mangle-on-delete a legitimate shortcut. The invariant either way: enforce uniqueness in the database, and add validation only for UX.
Compiled from questions I have run into in the field or been asked.
Comments
Sign in with your GitHub account to join the discussion. Comments are stored in GitHub Discussions.