Prisma error P3018 (A Migration Failed to Apply) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Run `npx prisma migrate status` to see which migration is in a failed state and why. If that does not apply, open the failing migration folder and read its migration.sql against the actual database state — the full checklist is below.
Error code: P3018
Official name: A Migration Failed to Apply
Service: Prisma
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve"Migration name: {migration_name}Database error code: {database_error_code}Database error:{database_error}
npx prisma migrate status
Shows every migration with a status of Applied / Pending / Failed so you know exactly which one is stuck.
# if the SQL actually ran against the DB despite the error:
npx prisma migrate resolve --applied 20260819000000_add_posts
# if it did not run:
npx prisma migrate resolve --rolled-back 20260819000000_add_posts
migrate resolve records the real state in _prisma_migrations without re-running SQL, unblocking the rest.
Use migrate status + migrate resolve as the recovery loop: inspect, decide, mark, re-apply.
npx prisma migrate status
# if migration X actually applied despite the error:
npx prisma migrate resolve --applied 20260819000000_fix_fk
# if it did NOT apply:
npx prisma migrate resolve --rolled-back 20260819000000_fix_fk
In CI, treat migrate deploy as the last step before smoke tests, and log migrate status on failure so a human can resolve.
- run: npx prisma migrate deploy
- run: npx prisma migrate status
if: failure()
Run migrations at deploy time (e.g. in a pre-start script), not on app boot, so a failed migration never takes down the process.
"start:prod": "prisma migrate deploy && node dist/main.js"
Most often this happens when the target database already contains a table with the same name but a different schema than the migration expects, or when a migration file references a column, constraint or enum that was manually dropped in the database.
Run `npx prisma migrate status` to see which migration is in a failed state and why.
This page documents fixes for: prisma-cli, github-actions, nestjs.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official Prisma documentation linked below and adds practical troubleshooting guidance on top.