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.
Error: P3009
migrate found failed migrations in the target database, new migrations will not be applied.
The `20240312_add_orders` migration started at ... failedCommon 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
- Inspect what the failed migration did and whether its changes are present.
- If you reverted its effects, mark it rolled back so Prisma re-applies it; if its changes are fully present, mark it applied.
- Re-run
prisma migrate deploy.
# 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_ordersFix 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.
npx prisma migrate deployHow to prevent it
- Run migrations against a disposable database in CI so a failure does not poison a shared environment.
- Test
migrate deployagainst a production-like snapshot before the real deploy. - Treat the
_prisma_migrationstable as the source of truth; do not edit schema by hand.