How to stop coding agents from hallucinating database schemas

by•

My coding assistant kept inventing columns when it wrote SQL queries for a new feature. It seemed sure. The code failed right away because those columns were not in our real production schema.

The solution was simple. I stopped letting the coding assistant guess. I put a plain text schema file directly into the project root folder. Then I changed the system prompt so that it reads that file before each database query task.

The errors fell away instantly because the coding assistant had a clear physical boundary to look at. It no longer relied on training memory and it used the exact table definitions instead.

When I build features with AI I never let the coding assistant depend on memory, for structural data. I always lock it to a source file.

What workflow do you use to keep your coding assistant aligned with your codebase structure?

3 views

Add a comment

Replies

Best

Same approach here, with one twist: my database is a document store with no schema at all, so there is nothing the agent could read from the database itself. The document shapes are written down in one notes file the agent reads before it touches data. And in code, every remote document goes through one model class where every field is nullable, which then maps it to a clean model with defaults. So if a guessed field ever slips through, it shows up in one place rather than across the app.