Blog

Unique Constraints Are Business Logic With Teeth

pre-checks lose races
Two identical business keys racing toward one database slot guarded by a steel constraint

if (!repository.exists(email)) insert(user) is not uniqueness. Two requests can both observe absence and both insert.

The database must arbitrate the race.

Encode The Actual Scope

Global email uniqueness uses one column. Tenant-local uniqueness needs a composite constraint.

alter table users
add constraint users_tenant_email_uq unique (tenant_id, normalized_email);

Normalization is part of identity. Decide case, whitespace, Unicode, and provider-specific email rules before constraint, not after duplicates arrive.

For soft deletion, decide whether a deleted identity remains reserved. A partial unique index may express active-only uniqueness in databases that support it.

Keep The Friendly Pre-Check If Useful

A pre-check can produce fast feedback. It cannot be authority. Catch the constraint violation from the insert and translate only the known constraint to a conflict.

Do not turn every integrity violation into “already exists.” A missing foreign key or check failure is different bug.

Generated Keys Are Not Business Identity

Two rows can have different surrogate IDs and still represent the same provider event, username, or idempotency key. Keep the numeric primary key if useful, then add a unique constraint for the domain identity.

Constraints make invalid state harder from every writer, including scripts and future services. That is business logic with a stronger execution engine.