AI SQL Generation

Press A from the grid or gA from the query pad to open the AI prompt. Describe what you want in plain English and the AI generates a SQL query against your current schema.

Setup

Configure your provider in the setup() call:

require('dadbod-grip').setup({
  ai = {
    provider = 'anthropic',           -- 'anthropic' | 'openai' | 'gemini' | 'ollama'
    model = 'claude-sonnet-4-6',      -- model name for the chosen provider
  }
})

Set your API key in your shell environment:

export ANTHROPIC_API_KEY=...   -- for Anthropic
export OPENAI_API_KEY=...      -- for OpenAI
export GEMINI_API_KEY=...      -- for Gemini

Ollama runs locally with no API key needed. Make sure Ollama is running before connecting.

Disable AI

Set ai = false to turn off all AI features:

require('dadbod-grip').setup({ ai = false })

With ai = false: schema pre-warm is skipped, the A and gA keymaps are not registered, and :GripFill is unavailable. SQL completion still works because it reads local schema data, not an external API. Use this in environments without API key access or where the plugin’s footprint must be strictly local.

How the context works

The AI receives:

  • Your current schema (table names, column names, types, PK/FK relationships)
  • Any existing SQL in the query pad (so you can ask it to modify a query rather than generate from scratch)
  • The current table name (when invoked from the grid)

Schema context is fetched lazily on the first AI invocation, not on connect. The first A or gA press may pause briefly while schema data is loaded. Subsequent invocations use the cached schema instantly.

Example prompts

From a clean query pad:

Show me all orders placed in the last 30 days with their customer name and total amount

To modify an existing query:

Add a filter to exclude cancelled orders

From the grid:

Find all rows where this column is above the average

Cross-database (federation session):

Join the Postgres customers table to the SQLite orders table and show me revenue by region

Supported providers

ProviderModelsKey env var
Anthropicclaude-opus-4-6, claude-sonnet-4-6, claude-haiku-4-5-20251001ANTHROPIC_API_KEY
OpenAIgpt-4.1, gpt-4.1-mini, gpt-4o, o3, o4-miniOPENAI_API_KEY
Geminigemini-2.5-pro, gemini-2.5-flashGEMINI_API_KEY
Ollamaany local model(none, uses localhost:11434)

Modify an existing query

When the query pad already has SQL, the AI uses it as context. You can ask it to refine rather than generate from scratch:

  1. Run a query and get results you want to extend
  2. Press gA from the query pad (or A from the grid to jump to the pad with context)
  3. Describe the change: “add a filter for the last 7 days” or “group by status”

The AI rewrites the existing SQL rather than replacing it wholesale.

The generated SQL goes into the query pad

After generation, the SQL appears in the query pad for review. Press <C-CR> to execute, or edit it first. Nothing runs automatically.

Both surfaces trigger AI

  • A from the grid: opens or focuses the query pad, pre-fills context for the current table
  • gA from the query pad: generates or modifies SQL in place

Both use the same model and schema context.

AI row fill

Press gA from the grid (not the query pad) to generate AI-populated staged rows. The AI reads the table schema, including column types and foreign key constraints, and produces realistic sample data that matches the structure.

Generated rows appear as staged inserts (green). Review them the same way as manual edits: press gl to see the INSERT statements, u to unstage individual rows, or a to apply.

For bulk generation, use the command form:

:GripFill 10

This stages 10 AI-generated rows at once. The rows respect unique constraints and produce varied, plausible values rather than repeating patterns.

Note: gA behaves differently depending on the surface. From the query pad, it modifies existing SQL. From the grid, it generates staged rows.