# How do I keep a unique constraint on a column like email while using soft deletes?

> Use a partial unique index on deleted_at IS NULL in PostgreSQL and a generated column in MySQL, adding app validation beside the constraint, not instead.

- Asked: 2026-11-21
- Answered: 2026-11-24
- Tags: Laravel, Eloquent, Data Modeling
- Source: https://muhammetsafak.com/just-ask/keep-unique-constraint-column-email-using-soft-deletes/
- Language: en-US
- Author: Muhammet Şafak

---
**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.


## 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

1. **PostgreSQL: a partial unique index is the cleanest path.** Enforce uniqueness only among live rows:

   ```sql
   CREATE UNIQUE INDEX users_email_active_unique
     ON users (email) WHERE deleted_at IS NULL;
   ```

   [Deleted rows don't enter the index](https://muhammetsafak.com/just-ask/should-i-use-a-partial-index-on-a-queue-table-where/); 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.

2. **MySQL has a trap: NULLs count as "distinct" in a unique index.** So `UNIQUE(email, deleted_at)` won't work — two active rows both have `deleted_at` NULL, MySQL treats them as different, and it lets duplicate actives through. The correct fix is a generated column:

   ```sql
   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()`.

3. **A simple but data-mutating alternative: mangle the email on delete.** On soft-delete, rewrite `email` to something like `bora+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.

4. **Add app validation too — but not instead of the DB.** In Laravel, validate against active rows:

   ```php
   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.

5. **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.*

## Related Reading

- [For comments across many models should I use a polymorphic relation or separate tables, given indexing and foreign keys?](https://muhammetsafak.com/just-ask/comments-polymorphic-relation-separate-tables-indexing-foreign-keys/) — Just Ask
- [Should I keep query business rules in Eloquent local scopes or in a repository layer?](https://muhammetsafak.com/just-ask/keep-query-rules-eloquent-local-scopes-repository-layer/) — Just Ask
- [Should I use chunkById instead of chunk when the same job also updates the rows it iterates?](https://muhammetsafak.com/just-ask/should-i-use-chunkbyid-instead-of-chunk-when-the-same-job/) — Just Ask
