Prisma P3018 "migration failed to apply" in CI
Prisma started applying a migration and the database rejected one of its statements. P3018 reports the database error message and the SQL that failed, then stops so later migrations do not run on a broken state.
What this error means
prisma migrate deploy fails with "P3018: A migration failed to apply" and embeds the underlying database error code and message (for example a duplicate column or constraint violation).
Error: P3018
A migration failed to apply. New migrations cannot be applied before the error is recovered from.
Migration name: 20240419_add_status
Database error code: 42701
Database error: ERROR: column "status" of relation "orders" already existsCommon causes
The migration SQL conflicts with the current schema
The statement tries to create an object that already exists, or violates a constraint, so the database returns an error (here 42701, duplicate column).
Bad or non-idempotent SQL in a hand-edited migration
A manually edited migration contains SQL that the target database cannot run as written, such as referencing a column that was renamed.
How to fix it
Read the database error and correct the SQL
- Note the "Database error code" and message; it is the real cause.
- Fix the migration SQL so it no longer conflicts (for a duplicate column, guard with a check or remove the redundant statement).
- Reset the disposable CI database and re-run, or mark the broken migration rolled back before re-applying the fixed version.
npx prisma migrate resolve --rolled-back 20240419_add_status
npx prisma migrate deployTest migrations against a production-like database
Apply migrations to a snapshot before the real deploy so a failing statement is caught before it blocks the pipeline.
How to prevent it
- Review migration SQL and avoid statements that assume objects do not exist.
- Run migrations against a clone of production before deploying.
- Do not hand-edit applied migrations; create a new one instead.