r/SQL • u/Filip1235813 • Sep 08 '26
MySQL Built a CLI that measures whether your implied foreign keys actually hold, then writes the result as context for coding agents
https://www.npmjs.com/package/dbtruth?activeTab=readme
Same thing kept happening to me with AI coding agents and Postgres. The agent reads the schema, sees orders.customer_id next to customers.id, assumes it's a clean relationship, and writes an INNER JOIN. If 12% of orders have a dangling or null customer_id, the query silently returns numbers that are wrong. Nothing throws. The schema looked fine.
So I wrote dbtruth. It connects read-only and, instead of dumping the schema into a context file, it does four things:
- Introspects schema and pulls samples
- A model proposes what the tables mean and which relationships probably exist
- Every one of those claims gets measured against the actual data
- Only what survives gets written to
./context/*.md, which the agent reads before writing SQL
Step 3 is the whole point. For a proposed join it reports the real match rate — orders.customer_id → customers.id holds for 88% of rows, 60 of 500 orders have no matching customer — and the context file says use LEFT JOIN, with the number attached. Under 50% gets dropped. In between gets marked broken and goes to the top of the report, because a relationship that half works is worse than one that doesn't exist.
Practical:
npx dbtruth, Node 20+, Postgres only- Read-only by construction, not by discipline: one module is allowed to import
pg, and a test asserts nothing else does. It never writes to your database. - It does call a model, so schema and low-cardinality sample values leave your machine. High-cardinality columns — emails, names, free text — are never sent. Visibility is decided by cardinality rather than by regex-guessing at PII. Don't point it at production data you can't send to a third party.
- MIT, source at github.com/FilipKalcic1/dbtruth#readme
Disclosure: it's mine, it's five days old, and about ten people have run it. None of the pieces are new — FK inference and data profiling both go back years, and there are other tools that build local context artifacts for agents. The part I care about is the rule that nothing unmeasured gets written down.
What I'd actually like to know: run it on a schema you know well, and tell me whether it found anything you didn't already know. That's the only signal that tells me whether this is worth continuing. Bug reports welcome too.
1
u/kantorcodes1 Sep 09 '26
when you rerun after a table is dropped or renamed, does dbtruth remove the old
context/tables/*.mdfile, or can stale table docs survive from the previous run? if the context is committed, stale files seem like the easy failure mode.1
u/Old-Biscotti3842 Sep 10 '26
The null vs dangling split is the one thing I'd want separated in that report. A null customer_id usually means the column is genuinely optional, guest checkout, imported orders, whatever, and LEFT JOIN is the right call. A non-null customer_id pointing at nothing is a different animal: a parent deleted without a cascade, or a migration replayed in the wrong order. The fix there isn't a join hint, it's a cleanup. Folding both into one 88% hides which of the two you actually have.
Two things from real schemas that break name-based inference, in case you want test material. Rails-style polymorphic columns (owner_id + owner_type) match every candidate table at 20-30% each, so your 50% rule drops them even though the pair is 100% consistent. And the *_id columns that were never foreign keys, stripe_customer_id, external_id, legacy_id, match nothing and land at the top of the "broken" list, which is the opposite of what they are.
If you want a schema with almost no declared constraints to try it on, the Discourse dump is public and has something like 23 foreign keys across 350+ tables. WordPress has zero. That's where "implied" is the whole game.