Skip to content
Latchkey

Prisma P3009 "migrate found failed migrations" in CI

Prisma found an entry in the _prisma_migrations table whose finished_at is null but with a logs error, meaning an earlier prisma migrate deploy failed partway. To avoid applying migrations on top of a broken state, Prisma stops with P3009.

What this error means

prisma migrate deploy aborts immediately with "P3009: migrate found failed migrations in the target database" and names the failed migration, before any new migration runs.

prisma output
Error: P3009

migrate found failed migrations in the target database, new migrations will not be applied.
The `20240312_add_orders` migration started at ... failed

Common causes

An earlier migration failed and was recorded

A previous deploy applied part of a migration, hit an error (bad SQL, a constraint violation), and Prisma wrote a failed row into _prisma_migrations. The table now blocks further deploys.

The database was partially migrated by hand

Someone applied or reverted SQL manually so the real schema and the migrations table disagree, leaving a migration stuck in the failed state.

How to fix it

Mark the migration rolled back or applied

  1. Inspect what the failed migration did and whether its changes are present.
  2. If you reverted its effects, mark it rolled back so Prisma re-applies it; if its changes are fully present, mark it applied.
  3. Re-run prisma migrate deploy.
Terminal
# you undid the partial change, so re-run it:
npx prisma migrate resolve --rolled-back 20240312_add_orders
# or, the change is fully in place already:
npx prisma migrate resolve --applied 20240312_add_orders

Fix the underlying SQL before re-deploying

If the migration failed because of bad SQL, correct the migration file, reset the database in a disposable CI environment, and let deploy run the corrected version.

Terminal
npx prisma migrate deploy

How to prevent it

  • Run migrations against a disposable database in CI so a failure does not poison a shared environment.
  • Test migrate deploy against a production-like snapshot before the real deploy.
  • Treat the _prisma_migrations table as the source of truth; do not edit schema by hand.

Frequently asked questions

What causes "Prisma "P3009""?
A previous deploy applied part of a migration, hit an error (bad SQL, a constraint violation), and Prisma wrote a failed row into _prisma_migrations. The table now blocks further deploys.
How do I fix Prisma "P3009"?
Mark the migration rolled back or applied

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card