In the admin console of an automotive marketplace I built, an admin deleted a vehicle brand by mistake and tried to add it back. The API refused: a brand with that slug already exists. The brand list said it didn't.
Both were telling the truth. This is the classic collision between soft deletes and unique constraints.
How it happens
Catalogue tables use Sequelize's paranoid mode. Deleting a row doesn't remove it; it sets deletedAt, and normal queries filter those rows out. That's what you want for catalogue data: old orders can still point at the brand they were placed under.
The slug column also has a unique index, because URLs and lookups depend on it. And the database doesn't know about paranoid. To PostgreSQL, the soft-deleted row is still a row, and its slug is still taken.
So:
- Admin deletes "Example Motors". The row stays, with
deletedAtset. - Admin creates "Example Motors" again with the same slug.
- The insert hits the unique index and fails.
- The list, which hides deleted rows, shows nothing to conflict with.
The options
A partial unique index, unique only where deletedAt IS NULL, makes the slug unique among live rows only. A new row can then be created while the old one stays soft-deleted. It's a good fit when the old row is a finished piece of history and the new one is genuinely a different thing. For a brand that's the same real-world company, you'd end up with two rows for one brand.
Restore instead of insert, which is what I chose. For catalogue data like brands and vehicle types, "re-adding" a deleted item almost always means "I want it back". So create now looks for a match, including deleted rows:
// Slug stays unique across soft-deleted rows, so revive a deleted match.
const existing = await this.vehicleBrandModel.findOne({
where: { slug: data.slug },
paranoid: false,
});
if (existing) {
if (!existing.deletedAt) {
throw new ConflictException("A brand with this slug already exists");
}
await existing.restore();
await existing.update({ is_active: true, logo_url: null, ...data });
return this.findById(existing.id);
}
Three outcomes, each correct:
- No match: insert as usual.
- A live match: a real duplicate, reported as
409 Conflictinstead of a raw database error. - A deleted match: restore it, apply the new values, and keep its id, so anything that referenced it keeps working.
One more guard: the find-then-restore sequence isn't atomic, so two simultaneous requests can both decide to insert. The unique index stays the final safety net, and its violation error should be mapped to the same 409 rather than surfacing as a 500.
Renames hit it too
The same collision appears when renaming to a slug that a deleted row still holds. There the deleted row is genuinely in the way, so it's removed for good before the rename. That's a deliberate trade-off: it's fine for a catalogue entry that nothing important points at any more, and wrong for anything tied to orders or audit history. A live clash still gets a 409:
const clash = await this.vehicleBrandModel.findOne({
where: { slug: data.slug, id: { [Op.ne]: id } },
paranoid: false,
});
if (clash?.deletedAt) await clash.destroy({ force: true });
else if (clash) throw new ConflictException("A brand with this slug already exists");
What I took away
- Soft delete is an application idea; uniqueness is a database rule. When you use both, decide on purpose how they interact, or users find out for you.
- Pick restore or a partial index per table. Restore fits when re-adding means "the same thing again": brands, categories, and user accounts, where identity, orders and history must stay attached to one row. A partial index fits when the old row is history and the new one is independent, such as a discount code reused for a new campaign, where the old campaign's redemptions must stay with the old row.
- Translate constraint errors. A
409with a clear message beats a500from an unhandled unique violation every time.
- #Database
- #Backend