Who's Enforcing Your Data Rules — the Database, or You?

RedHub AI Editorialupdated November 5, 202610 min read

A hardware shopkeeper stands with arms folded over a bin whose dividers are gone, its mixed screws and bolts lit red.
Jump to a section8

Every table in your app holds rules — who owns a row, what has to stay unique, what a price is — and each rule is being enforced by exactly one of two places: the database itself, or whatever code in your app happens to write to that table. When the database enforces it, the rule holds no matter how many different ways data gets written. When your code enforces it, the rule holds only as long as every write goes through the code that remembers to check — and the day something writes around that code, the rule quietly stops applying and nothing tells you.

TL;DR: A database can refuse a bad row, or it can just store whatever it's handed and trust your app to have gotten it right. Most schemas an AI builder generates default to the second kind without anyone deciding that on purpose. The fix isn't a security audit — it's going table by table and asking who is enforcing each rule, starting with whether the table even has a column naming who a row belongs to. That question comes before uniqueness, before correct data types, before anything else.

A place to put things, or a thing that can say no

Most people who didn't train as engineers think of a database as a filing cabinet: a place you put things so you can get them back later. That's true, and it's also the least useful way to think about it once your app has real users. A good database isn't just a place that stores things — it's a thing that can refuse them.

A filing cabinet doesn't care what you put in the folder marked "Invoices." A database with the right rules turned on does. It can refuse a row that claims to belong to a user who doesn't exist. It can refuse a second signup with an email address already in use. It can refuse a price that isn't a number. Or it can accept all three, quietly, because nobody told it not to — and an AI builder generating a schema from a feature description has no reason to turn any of that on unless you ask.

This is the idea the rest of this guide sits on top of: you cannot secure what you did not model. A rule about who's allowed to read a row has to compare that row against somebody. If no column names an owner, there is no comparison to make — the rule can't be written, tested, or turned on, no matter how badly you want it.

Two places a rule can live

Take three ordinary rules almost every app needs, and ask where each one lives.

RuleWho's enforcing itWhat happens when it's forgotten
Every row belongs to somebodyThe database, if an owner column references a real account and the database checks that reference. You, if the owner is just a value your code happens to write correctly.Nothing changes — until a second code path writes to the table without going through the first one. Then a row lands with the wrong owner, or no traceable owner at all, and the table accepts it without complaint.
Two things can't collide (two accounts, one email)The database, via a constraint that rejects the duplicate outright. You, if you only check before inserting.A duplicate is accepted the one time your check gets skipped — a retried request, a bulk import, two signups landing close enough together to both pass the check before either one finishes.
A price is money, not just a numberThe database, if the column has a type built for money and refuses anything else. You, if it's a plain number or text and your code keeps the convention.Nothing breaks the day you ship it. Text can't be added up by the database at all, and a number type built for approximate values can make two amounts that should match compare as different. It stays invisible until totals are compared and don't add up.

Notice that "you enforce it" isn't a lie in any of these rows. The rule does hold, today, in your app. It's just holding a different way than you probably assumed — and that difference only matters on the day something writes to the table that isn't the code you were thinking of. A migration script. An admin tool someone builds in month four. A second feature that touches the same table from a different angle. Code-enforced rules don't fail because they're wrong. They fail because they're not the only door into the room.

The question that comes before every other question

Here's where it gets more interesting than "some rules are stricter than others." One of the four questions worth asking about any table isn't a rule at all — it's a prerequisite for every rule that comes after it.

Before you can ask whether uniqueness is enforced, or whether a value is stored as the right type, you have to be able to say what a row is. Specifically: is this a record about somebody, or a list you decided on yourself? Plan tiers, country codes, status labels — these are reference lists. Nobody owns a plan tier. They need no owner column and asking for one would be manufacturing a problem that isn't there.

But a table that holds people's records — orders, comments, uploaded files, anything tied to a specific user — needs a column that names who each row belongs to. And if that column doesn't exist, the table drops straight to the bottom, before anyone even looks at whether the rest of it is well built. A rule your code holds up by hand ranks above it, on purpose: a hand-held rule can be moved into the table later, in one place, while a missing owner blocks every fix behind it.

This is the part that surprises people the first time they see it clearly: a table can have perfect data types, a unique constraint doing exactly its job, and zero duplicates in production — and still fail this test, because none of that answers the one question a permission rule needs answered. You can't write "only the owner may read this row" against a table that never says who the owner is. Making every other column on that table better doesn't change the answer, because the missing thing isn't a weak rule. It's the thing rules get written against.

"You enforce it" is a real state, not a failure

It's worth being precise about what this guide is and isn't arguing. Code-enforced rules aren't wrong. Plenty of real, working software runs for years on rules its code remembers to apply. The honest word for that state is fragile, not broken — it works, and it keeps working exactly as long as nothing changes about how the table gets written to, which is a bet you're making whether or not you noticed you made it.

The useful move isn't panicking about every code-enforced rule in your app. It's knowing which rules are which, so that when you do add a second way to write to a table — and you will, that's what growing an app looks like — you know which of your old assumptions just quietly stopped being true.

An hour with your table editor open

You don't need to memorize database theory to do this. You need your table editor open and the same four questions for every table: is it people's records or a list you seeded, does a column name the owner, is what you rely on being unique enforced, and are money and dates stored as themselves. Work in this order.

  1. List every table. Not just the ones you remember building on purpose — the ones an AI builder created along the way too. Test: open your table editor and write down every table name you see, including the ones you don't immediately recognize.
  2. Sort records from reference lists. A records table holds rows about specific people or things that happened. A reference list is something you decided, like plan names. Test: for each table, ask "who does a row belong to" — if the honest answer is "nobody, it's just a list," move on.
  3. Find the owner column on every records table. Look for a column that names who the row belongs to, and check whether the database verifies that reference or whether it's just a value someone wrote down. Test: try to trace one row's owner value back to a real account row — can the database prove it, or are you just trusting it?
  4. Fix the missing owners before anything else. Uniqueness and data types matter, but only once a table can say whose row it's looking at. Test: for any records table with no owner column, that's the first thing to add — every other fix on that table is waiting behind it.

If you want a literal sentence to hand your AI builder once you've found the gaps: "Every table that holds records tied to a specific user needs a column that names that user, and that column should reference a real row in the accounts table, not just hold a copied value." That one instruction covers the ownership step. Uniqueness and money get their own sentences in the guides linked below.

Scope: nothing here is a security review or a certification of your app. It's a way of looking at table shape — whether a rule has anywhere to attach to — and it grades the schema, never the person who built it.

What happens when you can't tell

Some answers won't be on the screen. You open a table and can't tell whether the owner column is a checked reference, or whether that email column has a uniqueness rule behind it. The tempting move is to assume the better answer. The honest move is to score the blank as the worst thing it could be hiding, because an answer you can't produce isn't evidence. A gap you've written down gets looked at. A blank you waved past doesn't.

That rule also keeps the exercise from being gamed: writing "I don't know" can never earn a better result than telling the truth would have. Where the unknown couldn't have changed the outcome either way — say, a table that has no owner column, where nothing about uniqueness could rescue it — it shouldn't count against you at all.

The Data Model Sanity Bench ($69) is these four questions as a spreadsheet. You list your own tables on its Bench tab (sixty rows, ten of them filled in with a worked example you clear and replace) and answer from dropdowns. Each table lands in one of four places: DATABASE ENFORCES IT, NO RULES NEEDED for a reference list, YOU ENFORCE IT, or NOTHING TO ENFORCE AGAINST for a records table with no owner column. The dashboard rolls those into one verdict — NOTHING TO BUILD ON if any records table has no owner, otherwise YOU ARE THE CONSTRAINT if your code holds up some rule, and MODEL HOLDS if every table enforces its own rules or needs none — and the "I couldn't find it" rule above is built in. When any table reads YOU ENFORCE IT or NOTHING TO ENFORCE AGAINST, it names one table to fix first, missing owners ahead of everything else. When no table reads either, it names none. It sees no rows. It reads back only what you tell it.

More in this guide

Frequently asked questions

What's the difference between a database constraint and application logic?

A database constraint is a rule the database itself checks on every write, no matter which part of your app is doing the writing. Application logic is a rule your code checks before it writes — which means the rule only holds for writes that go through that specific code. Both can enforce the same rule; they just fail differently when something goes wrong.

Is a database constraint always better than handling it in code?

Usually more durable, not automatically "better" in every sense — a code-enforced rule is a normal, working state, not a mistake. The point of this guide isn't to insist everything move into the database on day one. It's to make sure you know which rules currently depend on your code remembering, so you're not surprised later.

Do I have to fix every table before I do anything else?

No. Start with tables that hold people's records and have no column naming an owner — that's the one gap that blocks everything downstream, including any access rule you'd want to write later. Uniqueness and data-type issues are real but they're not prerequisites the way ownership is.

What about a table I created myself, like a list of plan tiers?

Reference lists don't need an owner column or a uniqueness rule tied to a person — nobody owns a plan tier. Asking for ownership on a table like that would be manufacturing a problem that isn't there. The question is whether the table holds records about people or a list you decided on.

Does this replace a security review of my app?

No. This is about the shape of your tables — whether a rule has a column to attach to at all. It doesn't look at your code, your access policies, or your infrastructure, and it isn't a certification that your app is secure.

Will my AI builder add these constraints if I just ask?

It can, and a specific request gives it the best chance. A vague request like "make my database secure" leaves the builder to guess which rule you meant. A specific one names the table, names the column, and says it should reference the accounts table. Either way, open your table editor afterward and confirm the constraint is there. The reply alone is not proof.

How it decides
Diagram of the Data-Model Sanity Bench: ten tables, a prerequisite ownership gate, and a sample reading NOTHING TO BUILD ON.

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