Assumed Unique: The Duplicate You Haven't Met Yet
RedHub AI Editorialupdated November 5, 20266 min read

Jump to a section7
A unique constraint tells the database to refuse a row that duplicates a value you've decided must be one-of-a-kind — two accounts with the same email, two invite codes that are the same string. Without one, your app hasn't proven the rule holds; it's never been tested by the duplicate yet, and a retried signup, a bulk import, or two requests landing close enough together can produce one you never planned for.
TL;DR: "This has never happened" and "this can't happen" are different claims, and it's easy to mistake one for the other. A unique constraint makes the second claim true. Checking before you insert only makes the first claim true, and the gap between them is exactly where duplicates come from.
The absence of a duplicate isn't evidence it can't happen
Ask most people whether two users could end up with the same email in their app, and the honest answer is "it hasn't happened so far." That's a true statement, and it's not the same statement as "it can't happen." The first is a record of what your app has been through. The second is a guarantee about what it will refuse. Most schemas an AI builder generates ship with the first kind by accident and nobody notices, because the first kind looks exactly like the second kind — right up until it doesn't.
A unique constraint is what turns "hasn't happened" into "can't happen." It's a rule on a column, or a combination of columns, that the database checks on every write: if the value you're trying to save already exists somewhere else in that column, the write is refused, full stop, regardless of which part of your app tried to make it.
Why "I check before I insert" isn't the same guarantee
The intuitive way to prevent duplicates without a database constraint is to check first — query for an existing row with that email, and only insert if nothing came back. This works most of the time, and that's exactly why the gap is so easy to miss: it looks solid in every normal test.
It breaks in a specific, narrow window: two requests that both check "does this email exist" at nearly the same moment, both get back "no," and both proceed to insert. Neither one did anything wrong on its own. The check was accurate at the instant it ran. It just wasn't accurate anymore by the time the second insert landed, because nothing locked out the second request while the first one was still in flight. This is the kind of bug that's hard to catch by testing normally, because it needs two things to happen close enough together — which is exactly the situation a retried request, a double-click, or a bulk import is good at creating and a manual test is not.
A unique constraint doesn't have this gap, because it isn't a check that runs before the write and hopes nothing changes in between. It's a rule the database enforces at the moment of the write itself, for every write, from any code path, including the ones you haven't built yet.
What's assumed unique in a typical schema
The usual suspects are the ones that feel too obvious to need a rule: email addresses on an accounts table, invite or referral codes, slugs used in a URL, a combination like "one review per user per product." Each of these is a value your app almost certainly treats as unique somewhere in its logic — a login flow that looks up by email, a share link that assumes the slug resolves to exactly one thing. The database enforcing that same assumption is what keeps the assumption from quietly breaking the moment a second code path stops respecting it.
Combination uniqueness is worth calling out specifically, because it's easy to check the wrong half. "One review per user per product" isn't a rule about the user column alone, or the product column alone — it's a rule about the pair. A constraint on just one of those columns would block something you didn't mean to block, or miss the actual duplicate entirely.
What doesn't need this
Not everything benefits from a uniqueness rule, and it's worth not manufacturing problems where none exist. A table of country codes or plan tiers you seeded yourself is a list you wrote once and add to by hand, with no path for a user to write to it. A duplicate there would be your own typo, and you'd see it. Ask whether anything outside your direct control writes to the column. If nothing does, this isn't where your attention belongs.
Plenty of records tables also have nothing that needs to be unique. An activity log can hold the same event twice on purpose. Saying so is a real answer, and it isn't a gap.
Checking your own tables
- List the values your app already assumes are unique. Email, invite codes, slugs, any "one of these per that" combination. Test: for each one, ask where in your code that assumption currently lives.
- Check whether it's a database constraint or a check-before-insert. Your table editor's schema or index view usually shows a real constraint or unique index directly. Test: look for it explicitly — "I don't see one" and "there isn't one" are the same answer here.
- Add the constraint for anything that would hurt if it duplicated. Especially anything more than one code path can write to. Test: ask whether an admin tool, an import script, or a future feature could write to this table without going through your existing check.
A literal sentence for your AI builder: "Add a unique constraint on the email column of the accounts table, so the database rejects a duplicate signup instead of relying on the signup form to catch it first."
The Data Model Sanity Bench ($69) asks this as one of its four questions per table, with four answers: enforced, assumed only, nothing here needs to be unique, or can't find. Assumed only reads YOU ENFORCE IT. So does can't find, because the bench scores an answer you couldn't produce as the worst it could have been. In its worked example, a team-invites table (T-09) lands there for exactly that reason. It takes one answer per table, so if anything on a table you rely on being unique is only assumed, that's the honest answer for the row. On a table you mark as a seeded reference list, the bench doesn't use this answer at all and the row reads NO RULES NEEDED. It doesn't look at your rows, so it can't tell you whether duplicates already exist.
More in this guide
Frequently asked questions
What is a unique constraint?
A rule attached to a column, or a combination of columns, that makes the database refuse a write if the value already exists elsewhere in that column. It's enforced on every write, regardless of which part of your app is doing the writing.
Isn't checking for a duplicate before I insert good enough?
It handles the ordinary case well and fails in a narrow one: two writes checking at nearly the same moment can both pass the check before either finishes inserting. A database constraint doesn't have that gap, because it's enforced at the moment of the write itself.
Can I add a unique constraint if the table already has duplicates in it?
Typically the database will refuse to add the constraint until the existing duplicates are resolved, since the rule can't apply retroactively to data that already violates it. Check your specific database's documentation for how it reports and handles this case.
What should I check for uniqueness that I might be missing?
Beyond the obvious single-column cases like email, look for combination rules — "one of X per Y" — which need a constraint across both columns together, not either one alone.
Does this tell me if my app currently has duplicate data?
No. It tells you whether a duplicate is currently possible going forward, not whether one already exists in your data. Finding existing duplicates is a separate, one-time check against your actual rows.


The gate this post refers to, drawn from the tool’s own logic. See the tool.