afterbuild/ops
ERR-336/Prisma · Database
ERR-336
Error: P3009 migrate found failed migrations in the target database

appears when:When a previous prisma migrate deploy failed mid-way, schema drift exists, or the shadow DB user lacks create privileges

Prisma migration failed in production

A partial migration is worse than no migration. Prisma refuses to proceed until you mark the stuck one as applied or rolled back — no amount of retries will fix it alone.

Last updated 8 August 2026 · 9 min read · By Hyder Shah
Direct answer
Run prisma migrate status against the production DATABASE_URL. If it shows a failed migration, either finish the DDL manually and mark it --applied, or revert the partial DDL and mark it --rolled-back. Never re-run migrate deploy on a stuck migration without resolving first — it fails on the same step every time.

How do I fix Prisma migration failed in production?

scripts/resolve-migration.sh
bash
01# Resolve a stuck migration in production (read-only diagnosis first)02npx prisma migrate status03 04# If status shows a migration in the "failed" state:05# 1. Manually finish or revert the DDL in a transaction06psql "$DATABASE_URL" -c "BEGIN; -- inspect the partial state; ROLLBACK;"07 08# 2. Mark the failed migration as resolved, not re-applied09npx prisma migrate resolve --applied "20260410_add_index_users_email"10# or if the DDL was rolled back manually:11npx prisma migrate resolve --rolled-back "20260410_add_index_users_email"12 13# 3. Now safe to run remaining migrations14npx prisma migrate deploy15 16# For drift (prod has schema not in migrations):17# Option A: baseline from prod18npx prisma db pull19# review the diff, commit as an empty migration marked applied20npx prisma migrate resolve --applied "0_init"
The three-step recovery: status, manual finish or revert, resolve — then continue deploying

Not a developer? We fix this for you at a fixed price: $299 emergency triage, done in 48 hours or fully refunded. Not sure what else is broken? Send the repo for the $49 audit.

What if the quick fix doesn't work?

01 · Provide an explicit shadow database for managed Postgres

.env
bash
01# .env — pointing at a separate empty DB the Prisma user owns02DATABASE_URL="postgres://user:pw@neon-main.example.com/appdb"03PRISMA_MIGRATE_SHADOW_DATABASE_URL="postgres://user:pw@neon-shadow.example.com/shadow"04 05# On Neon: create a second branch called "shadow" and use its connection string06# On Supabase: create a second project for migrations-only use07# On Railway/Fly: spin up a throwaway Postgres and wire only in dev
Managed Postgres users rarely have CREATE DATABASE — give Prisma a dedicated shadow

02 · Baseline an existing production database

scripts/baseline.sh
bash
01# When prod has schema that is not in any migration (drift)02# 1. Pull the current schema into schema.prisma03npx prisma db pull04 05# 2. Create an initial migration from the current schema06mkdir -p prisma/migrations/0_init07npx prisma migrate diff \08  --from-empty \09  --to-schema-datamodel prisma/schema.prisma \10  --script > prisma/migrations/0_init/migration.sql11 12# 3. Mark it applied without running it13npx prisma migrate resolve --applied 0_init14 15# Now future migrations build on this baseline
Accept current prod as the new baseline — safest path when history is unknown

03 · CI check that catches drift before deploy

.github/workflows/migrations.yml
yaml
01# .github/workflows/migrations.yml02name: Check migrations03on: pull_request04jobs:05  check:06    runs-on: ubuntu-latest07    steps:08      - uses: actions/checkout@v409      - uses: actions/setup-node@v410        with: { node-version: 20 }11      - run: npm ci12      - name: Migration status vs production13        run: npx prisma migrate status14        env:15          DATABASE_URL: ${{ secrets.PROD_READONLY_DATABASE_URL }}16      - name: Migration diff must be empty or additive17        run: |18          # Prisma ORM v7 removed --from-url: use --from-config-datasource19          # with the datasource defined in prisma.config.ts instead20          npx prisma migrate diff \21            --from-url "${{ secrets.PROD_READONLY_DATABASE_URL }}" \22            --to-schema-datamodel prisma/schema.prisma \23            --exit-code
Block any PR that would produce drift or a destructive migration against production

Using migrate status as a CI gate, not as a human-read command

The exit code is what makes prisma migrate statusscriptable, and the overloading is what makes naive scripts lie. Five conditions share exit 1 — pending migrations, diverged history, a missing migration table, failed migrations, and a plain connection error — so a pipeline that treats non-zero as “you forgot to migrate” will report a forgotten migration when what actually happened is that the runner could not reach the database. Capture stdout and branch on the printed marker when the distinction matters; let the exit code stand alone only in a pipeline where an unreachable database should also fail the build.

Run the gate with a read-only production credential. migrate status issues no DDL, so a role with SELECT on _prisma_migrations is sufficient, and a role with less than write access cannot turn a diagnostic step into an accidental deploy. That is also why the shadow database never enters this path: Prisma documents the shadow database as a requirement of migrate dev, which builds and drops a clean copy to verify a migration, and documents none for migrate status.

When _prisma_migrations is clean but the schema is not

prisma migrate status reads two things: the filenames and checksums under prisma/migrations, and the rows in _prisma_migrations. It never reads your tables. Someone who adds a column with hand-written SQL changes the schema without changing either input, so status keeps printing Database schema is up to date! while the live database has quietly stopped matching the migrations that supposedly produced it. The failure surfaces one deploy later, on a migration whose ALTER assumes the pre-edit shape.

prisma migrate diff is the command that reads the database itself, which is why it — not status — is the honest drift detector. Diff the live schema against your schema.prisma and treat any non-empty output on a branch nobody intended to change as evidence of an out-of-band edit. The CI workflow below runs both, and the order matters: status first to catch the cheap case, diff second to catch the expensive one.

Tried the fix and still broken?

Send your app URL. A senior engineer records a 10-minute teardown.

Free, and fully async — no call to book. A senior engineer walks your app on video: what’s broken, what’s risky, what to fix first. In your inbox within 24 hours. Want the code read line by line instead? Send the repo for the $49 audit.

Why do AI-built apps hit Prisma migration failed in production?

Prisma tracks every migration in a meta table called _prisma_migrations. Each row records the migration name, the SQL applied, the checksum, and a status. When you run prisma migrate deploy, Prisma reads the folder of migration files, compares against the rows in _prisma_migrations, and applies anything missing. If one step of a migration fails — say, a CREATE INDEX CONCURRENTLY that hit a lock timeout — Prisma writes a row with finished_at = null and marks it failed. On the next deploy, Prisma sees the stuck row and refuses to continue. The reasoning is correct: re-running a migration that partly succeeded could corrupt the schema.

AI-generated code adds three failure modes on top. First, scaffolds frequently ship with destructive changes in migrations (column drops, type narrowing) that Prisma refuses by default on production databases. You get Data loss warning: this migration will... and the deploy aborts. Second, Lovable and Bolt tend to create a fresh schema.prisma in every session, overwriting your migration history. Third, when working against Neon branches, the branch baseline may diverge from main, so the migration files reference a schema state that does not exist on main.

The dev-only variant is the shadow database error. prisma migrate dev creates a throwaway database to verify the migration applies cleanly to a blank slate. If your Postgres user does not have CREATE DATABASE privileges — as is standard on managed providers like Supabase and Neon — the shadow step fails with permission denied to create database. The fix is to provide a dedicated shadow database via PRISMA_MIGRATE_SHADOW_DATABASE_URL, pointing at a separate empty database the user can drop and recreate.

The last failure is drift: production has tables or columns that are not in any migration. That happens when someone runs raw SQL against prod, or when a previous deployment created schema via prisma db push instead of a tracked migration. migrate deploy detects drift, refuses to proceed, and asks you to baseline. Resolving drift is manual: either db pull the current prod schema into an initial migration and mark it applied, or revert the drifted changes to match the migration history.

Diagnose Prisma migration failed in production by failure mode

prisma migrate status answers a narrower question than most people think it does, and its exit code answers a narrower one still. The Prisma CLI reference states that in versions 4.3.0 and later the command exits with code 1 for five distinct conditions, so the number alone cannot tell them apart — the stdout marker can. Match what you saw printed, not what the shell returned.

What migrate status printsExit codeWhat it meansNext command
Database schema is up to date!0Every migration file has a matching row in _prisma_migrations. Says nothing about whether the live tables still matchprisma migrate diff, if you suspect hand-run SQL
Following migration have not yet been applied1Pending migrations — files on disk with no row in the databaseprisma migrate deploy
The migrations from the database are not found locally in prisma/migrations1Diverged history — the database has applied migrations your checkout does not containRebase the branch before touching the database
No migration table found / failed migrations found1Either an unbaselined database, or a migration stuck with finished_at nullprisma migrate resolve --applied | --rolled-back
Connection error1The command never reached the database — indistinguishable from a real drift finding by exit code aloneFix DATABASE_URL before trusting any status result

Which AI builders ship Prisma migration failed in production?

How often each AI builder ships this error and the pattern that produces it.

AI builder × Prisma migration failed in production
BuilderFrequencyPattern
LovableEvery schema rewriteRegenerates schema.prisma from scratch, overwrites migration history
Bolt.newCommonUses prisma db push in dev, then tries migrate deploy in prod — drift
CursorCommonSuggests destructive column renames without a data-preserving intermediate step
Base44SometimesHard-codes shadow DB URL to main DATABASE_URL — corrupts prod
Replit AgentRareSkips migrations entirely, ships with raw SQL on startup

How do I stop Prisma migration failed in production recurring?

Still stuck with Prisma migration failed in production?

Emergency triage · $299 · 48h turnaround
We restore service and write the root-cause report.
start the triage →

Prisma migration failed in production questions

What does prisma migrate status return when migrations are pending?+
It prints the pending migration names to stdout and exits with code 1. The Prisma CLI reference states that in versions 4.3.0 and later, prisma migrate status exits with exit code 1 in the following cases: a database connection error occurs; there are migration files in the migrations directory that have not been applied to the database; the migration history in the migrations directory has diverged from the state of the database; no migration table is found; failed migrations are found. All five collapse to the same exit code, so the number tells you something is wrong and only the stdout tells you which of the five it is.
How do I fail a CI job on pending migrations without failing on a clean database?+
You cannot do it on the exit code alone — exit 1 covers pending migrations, drift, a missing migration table, failed migrations, and a connection error indiscriminately. A clean database at the head of the history exits 0 and prints 'Database schema is up to date!', so the exit code is a usable gate only when a connection failure should also break the build, which in CI it usually should. When you need to distinguish, capture stdout and branch on the marker text rather than on the status code, and run the check against a read-only production credential so the gate can never mutate the database it is inspecting.
Does prisma migrate status need shadow database access?+
No. The Prisma CLI reference documents the shadow database as a requirement of prisma migrate dev, which creates and drops a throwaway database to verify a migration against a clean schema. It documents no such requirement for migrate status, which compares the migration files on disk against the _prisma_migrations table in the target database and does nothing else. That is why migrate status is safe to run against production with a low-privilege user, and why a 'permission denied to create database' error during a status check means something else in your toolchain invoked migrate dev.
Why does migrate status disagree with migrate deploy after a manual DB edit?+
Because they read different things. migrate status compares filenames and checksums in prisma/migrations against rows in _prisma_migrations — it never inspects your actual tables. Raw SQL run by hand changes the tables without touching that table, so status can report 'Database schema is up to date!' while the live schema no longer matches what those migrations produce. The next migrate deploy then fails on a step that assumes the pre-edit shape. To see the real divergence, run prisma migrate diff between the live database and your schema file: that is the command that reads the database itself.
What is Prisma schema drift and why does it block migrations?+
Schema drift is when your production database has tables, columns, or indexes that are not in the Prisma migration history. Drift usually comes from someone running raw SQL against prod without a migration, or switching branches that had different migrations applied. Prisma detects drift when you run migrate deploy, refuses to proceed, and tells you to investigate. The fix is either prisma db pull + baseline, or prisma migrate resolve --applied for each known-good migration.
Why does Prisma need a shadow database?+
Prisma creates a throwaway shadow database during prisma migrate dev to verify that your migration, when applied to a clean copy of the schema, produces the exact model your schema.prisma defines. It is a safety net against handwritten SQL that drifts from the model. In production (migrate deploy) the shadow DB is not used — so shadow DB errors almost always come from dev, not prod. Use the PRISMA_MIGRATE_SHADOW_DATABASE_URL env var to point at a separate database the dev user can create and drop.
How do I resolve a failed migration in production?+
Run prisma migrate status to see which migration is stuck. If the partial DDL is safe to keep, manually finish applying it with raw SQL, then run prisma migrate resolve --applied <migration_name> to mark it complete in the _prisma_migrations table. If the partial changes need to be reverted, roll them back manually, then prisma migrate resolve --rolled-back <name>. Never re-run migrate deploy on a failed migration without first resolving it — it will fail on the same step again.
Why does my migration work on Neon branch but fail on main?+
Neon branches are copy-on-write clones. If you developed against a branch that was created before a previous migration ran on main, your local history is out of sync with main's history. When Prisma compares _prisma_migrations on main vs your migration folder, names do not line up. Use prisma migrate status against the main branch URL to see the real state, then either apply missing migrations in order or reset the branch from main before continuing.
How much does an Afterbuild Labs Prisma audit cost?+
Our Auth, Database and Integrations service covers Prisma migration recovery. For a drifted production database, how long diagnosis and resolution take depends on how far prod has drifted from the migration history. Fixed-fee. Deliverables: clean migration state, a documented runbook, and a CI check that runs prisma migrate diff against prod on every PR to catch future drift before it lands.
Next step

Ship the fix. Keep the fix.

Emergency Triage restores service in 48 hours. Break the Fix Loop rebuilds CI so this error cannot ship again.

About the author

Hyder Shah leads Afterbuild Labs, shipping production rescues for apps built in Lovable, Bolt.new, Cursor, Replit, v0, and Base44. our rescue methodology.

Sources