Solution skill
P3018: A migration failed to apply. New migrations cannot be applied before the error is recovered from.
Complete solution
Fixes Prisma error P3018 'A migration failed to apply' by deciding whether the failed migration's changes actually landed, then marking it rolled back or applied with migrate resolve. Use when migrate deploy stops with P3018 and new migrations are blocked. Not for P3006 shadow database failures or P3005 non-empty schema errors.
TL;DR: A migration died halfway and Prisma froze the history until you resolve it. Check whether its changes actually landed in the database, fix the partial state by hand, then run prisma migrate resolve --rolled-back "[name]" (or --applied if the changes did land) and redeploy.
Error: P3018: A migration failed to apply. New migrations cannot be applied before the error is recovered from.Steps
Find the failed migration:
npx prisma migrate statusExpected: it names the failed migration and shows everything after it as blocked.
Inspect what actually landed. Compare the migration SQL against the real database (for example, check whether the table or column exists). Expected: you can answer yes or no to 'did the failed migration's changes get applied?'
Resolve it honestly:
Changes did NOT land: repair any partial state manually, then
npx prisma migrate resolve --rolled-back "20240102_add_orders"Changes DID land (the failure was after the DDL, e.g. in a data step):
npx prisma migrate resolve --applied "20240102_add_orders"
Expected:
Migration 20240102_add_orders marked as rolled back.(or applied).Fix the migration file if it was buggy, then redeploy:
npx prisma migrate deployExpected: the fixed migration applies and later ones follow.
When to use
- The exact code is P3018 and
migrate statusshows a failed migration blocking the queue - A deploy died mid-migration (OOM, statement timeout, bad SQL)
When not to use
- P3006: the failure was on the shadow database, the real database is untouched
- You want to undo a migration that applied cleanly: write a new migration instead, Prisma has no down migrations
Compatibility
Prisma Migrate 3.x through 6.x. PostgreSQL, MySQL, SQL Server.
Variant phrasing: P3018 with database error: relation already exists
The DDL half-applied before the crash. Drop the partially created objects by hand, then resolve as rolled back.
Why it happens
Prisma records each migration as failed in _prisma_migrations and refuses to run anything newer until a human declares the outcome. --rolled-back means 'I cleaned up, pretend it never ran'; --applied means 'the changes are really there, record it as done'. Picking the wrong one corrupts the history, so the manual inspection in step 2 is the whole job.
Edge cases
- Never edit a failed migration file and re-run deploy without resolving: Prisma checksums the file and the history row stays failed.
- On production, practice the manual repair on a restored copy first.
- If the failed migration is the very first one and the database is otherwise empty,
migrate reseton a scratch database is simpler than surgery.
Evidence
A deploy failed with P3018 after a migration crashed halfway: the database was left in a half-applied state and every later deploy refused to run. The recovery sequence that worked, documented in the project's learnings: inspect what the failed migration actually changed, repair or revert the partial changes manually, then run prisma migrate resolve --rolled-back against the failed migration so Prisma unblocks the history, and redeploy. After that, pending migrations applied cleanly. Source: https://github.com/devwithabhishek/kridha/blob/HEAD/docs/LEARNINGS.md
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_iSxa_kV6rcMpgT732pEFDQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">P3018: A migration failed to apply. New migrations cannot be applied before the error is recovered from. <span>— Vectle</span></a>