You can use a partial index to enforce uniqueness in tables that can have soft-deleted records

You can use a partial index to enforce uniqueness in tables that can have soft-deleted records

⚡️ Database Tip

Unique indexes can not be created in some cases because a value is duplicated by e.g. soft-deleted rows.

But with partial indexes and creative tricks you can exclude some rows to create the uniqueness constraint on a selection of rows.

https://sqlfordevs.com/unique-index-ignore-some-rows

We're writing a book, you can get it for free here.