How Migrations Work
When you ask the AI to change your database through chat, it doesn’t modify the schema directly. Instead, it creates a migration file containing the SQL needed to make the change.| Concept | What It Means |
|---|---|
| Migration file | A SQL file that describes a specific schema change |
| Up direction | The SQL that applies the change (e.g., create a table) |
| Down direction | Conceptually, the SQL that would undo the change (e.g., drop the table) |
| Applied | The migration has been run against your database |
| Pending | The migration exists but hasn’t been applied yet |
Why Migrations Instead of Direct Changes?
Migrations give you:- A history of every schema change
- Reproducibility — you can recreate the database from scratch by running all migrations in order
- Visibility — you can see exactly what SQL will run before it runs
- Safety — changes are explicit and reviewable, not hidden
Viewing Migrations
The Migrations view in the Database tab shows you all migrations for your project:- Pending migrations — Created by the AI but not yet applied to your database
- Applied migrations — Already run against your database
- Migration SQL — Click any migration to see the exact SQL it contains
- Status indicators — Quickly see which migrations are pending and which are applied
- Timestamps — See when each migration was created and when it was applied
Applying Migrations
When the AI creates a migration, it appears as pending. To apply it:Review the migration SQL
Click the pending migration to see the exact SQL it will execute. Make sure
it matches what you expect.
Apply the migration
Click to apply the pending migration. The SQL executes against your Supabase
database.
What Happens When You Apply
The migration SQL executes against your live Supabase database. If it succeeds, the migration is marked as applied. If it fails, you get an error message explaining what went wrong, and the migration stays pending so you can address the issue.Common Migration Operations
Here are the kinds of schema changes that generate migrations:Creating a New Table
Adding a Column
Creating an Index
Enabling Row Level Security
Adding a Foreign Key
Migration Best Practices
Follow these patterns to keep your database changes clean:- Let the AI generate migrations — You rarely need to write them manually. Describe what you want in chat and let the AI create the migration.
- Review before applying — Always look at the SQL before applying. Make sure it does what you expect.
- Apply in order — If you have multiple pending migrations, apply them in the order they were created. Later migrations may depend on earlier ones.
- One change per migration — Each migration should do one logical thing. Adding a table is one migration. Adding RLS policies to that table is another. This keeps things easy to understand and debug.
Troubleshooting Failed Migrations
Duplicate column or table name
Duplicate column or table name
The migration tries to create something that already exists. This can happen
if you manually created a table or column outside of migrations. Ask the AI
to check the current schema before generating new migrations.
Foreign key reference not found
Foreign key reference not found
The migration references a table or column that doesn’t exist yet. This
usually means migrations are being applied out of order, or a prerequisite
migration failed. Check the migration order and apply any earlier pending
migrations first.
RLS policy conflict
RLS policy conflict
A policy with the same name already exists on the table. Policy names must be
unique per table. Ask the AI to use a different policy name, or drop the
existing policy first.
Permission denied
Permission denied
Your Supabase API key might not have the right permissions for schema changes.
Make sure you’re using the service role key for migration operations.
Syntax error in migration SQL
Syntax error in migration SQL
The AI occasionally generates SQL with minor syntax issues. Copy the SQL from
the migration, paste it into the SQL Editor, fix the syntax, and run it
manually. Then ask the AI to regenerate the migration with the correction.
Migrations are generated by the AI when you ask for database changes through
chat. You rarely need to write them manually.
Related
Database Overview
Learn about all the database management tools available in Nativeline.
SQL Editor
Write and execute SQL queries directly against your database.