There's a specific failure mode that every AI coding user encounters eventually: you ask the agent to write a database query, and it confidently writes one that references columns that don't exist. Or it invents a field name based on what seems reasonable from context. Or it uses the right table but the wrong relationship, and the query runs but returns nonsense.
This isn't a prompting problem. It's an information problem. The agent is working from memory and inference, not from a real schema. It doesn't know what your tables actually contain — so it guesses.
The fix is straightforward: give the agent a real schema to read. This article explains how, and why it produces such a dramatic improvement in code quality.
Why AI Coding Tools Make Up Data
Cursor, Claude Code, and Codex are all large language models at their core. They've been trained on enormous amounts of code, including database schemas, SQL queries, and API definitions. When you ask them to write a query against "a users table," they draw on that training to produce something plausible.
Plausible isn't the same as correct. Your users table might have user_id instead of id, created_at as a timestamp instead of a date, a profile_image_url field that the model never guesses, and a foreign key to a subscriptions table that the model doesn't know exists. The model writes what it thinks a users table should look like — not what yours actually contains.
The result: queries that look right, pass a syntax check, and fail at runtime.
The Fix: Schema Introspection
When an AI coding tool has access to a real, machine-readable schema, it reads the actual structure instead of guessing. Every field name, type, constraint, and relationship is available. Generated queries reference columns that exist. Foreign keys are correct. Required fields are included.
This is why connecting a database is the single highest-leverage configuration change you can make to Cursor or Claude Code. The improvement in code accuracy isn't marginal — it's the difference between queries that work on the first try and queries that need three rounds of debugging.
Option 1: Connect via MCP (Works Everywhere)
The Model Context Protocol provides a standard way to connect any database to any MCP-compatible AI coding tool.
For a raw Postgres database:
- Add a Postgres MCP server to your tool's configuration. For Cursor, this goes in .cursor/mcp.json:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:password@host:5432/dbname"]
}
}
}
For Claude Code, add the server via /mcp in the CLI or in your claude_desktop_config.json.
- The MCP server exposes schema inspection tools that the agent uses automatically when you ask database-related questions.
What you get: The agent can read your schema, query tables, and write queries against the actual structure. It still generates SQL directly, so you're responsible for ensuring the queries are safe.
Limitation: Raw SQL access is a developer tool. It gives you data access, not a complete backend — no auth layer, no permissions model, no server-side logic. It's also only as safe as the database credentials you provide.
Option 2: Connect via a Backend Plugin (GraphQL Introspection)
If you're building an application — not just querying a database — a better approach is to connect a backend that exposes a self-documenting GraphQL API with schema introspection.
GraphQL introspection is a particularly good fit for AI coding tools because the schema is machine-readable by design. The agent queries the API itself to discover what tables, fields, types, and operations are available. It doesn't need a separate schema file or documentation — the schema is the API.
Momen is a visual backend that exposes its entire data model — tables, relations, Actionflows, and permissions — as a typed GraphQL API with introspection enabled. When you connect Momen to Cursor or Claude Code via the Plugin or MCP, the agent reads your actual data model and generates queries and mutations that reference columns and types that actually exist.
For Claude Code:
Install the Momen Plugin from the @momen marketplace. The Plugin includes a momen-platform Skill, a momen-mcp CLI, and a SessionStart hook:
/plugin marketplace add momen-tech-org/momen-nocode-plugin
/plugin install momen-nocode@momen
For Cursor:
Add to .cursor/mcp.json:
{
"mcpServers": {
"momen": {
"command": "npx",
"args": ["-y", "momen-mcp@latest", "mcp"]
}
}
}
Cursor reads the visual data model through GraphQL introspection and writes queries against a schema it fully understands. Pair this with a .cursor/rules snippet that tells Cursor to always introspect the schema before writing database operations — see The Simple Setup That Makes Cursor Write Correct Code Every Time for the exact configuration.
What you get: Queries and mutations that reference real columns, correct types, and actual relationships. The agent doesn't need to guess — and because the schema is visual and editable, you can update it and the agent's next session automatically sees the new structure.
For a real example of this pattern in production, the Claude Code + Momen hackathon build shows exactly how schema-first development changes what the agent can do.
The Schema-First Workflow
Once your agent has a real schema to read, the development flow changes:
- Define your data model first. Before writing any frontend code, set up the tables, relations, and constraints. For Momen, this is done visually in the editor.
- Let the agent introspect. The first time the agent accesses the schema — either via a SessionStart hook or on the first database-related prompt — it loads the full structure.
- Generate against the real schema. Every query, mutation, and API call the agent writes is based on what's actually there. No more invented field names.
- Update the schema, not the prompts. When you add a table or a field, the agent's next session automatically sees the new structure. You don't need to update a documentation file or remind the agent what changed.
This is why the schema-first approach produces consistent results over time. The agent isn't working from a snapshot you wrote at setup — it's reading the live, current state of your backend every session.
Which Option Should You Use?
| Situation | Best Option |
|---|---|
| Querying an existing database as a developer | Postgres MCP |
| Building a new app with auth, permissions, and logic | Backend Plugin (Momen) |
| Non-technical builder who needs a real data layer | Backend Plugin (Momen) |
| Existing Supabase or Postgres project, adding AI tools | Postgres MCP or Supabase MCP |
| Need server-side Actionflows callable by the agent | Backend Plugin (Momen) |
Summary
The most reliable way to stop your AI coding tool from making up database fields is to give it a real schema to read. A Postgres MCP server does this for an existing database. A backend Plugin with GraphQL introspection does this for a new application — and adds auth, permissions, and server-side logic to the same package.
Either way, the result is the same: the agent writes queries that reference columns that exist, against types that are correct, with relationships that are real. That's not a minor improvement. It's the difference between code that works and code that looks like it works.
Top comments (0)