# Prisma P3009: migrate found failed migrations in the target database

> An earlier prisma migrate deploy started a migration and never finished it, and Prisma recorded that in the _prisma_migrations table. Until that record is resolved, no new migration is applied. Find the migration and why it failed, fix the SQL or the database, mark it with prisma migrate resolve --rolled-back or --applied, and run migrate deploy again, before the app starts rather than in the build.

Source: https://livops.dev/en/guides/error-prisma-p3009-failed-migrations

By Livops team, the people who build and run Livops. Updated 3 December 2026.

The message, as Prisma's source defines it[3], with your migration's name and time filled in:

```text
P3009

migrate found failed migrations in the target database, new migrations will not be applied. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
The `20261201091400_add_user_email` migration started at <time> failed
```

You see it from `prisma migrate deploy`, usually on the deploy after the one where a migration broke. This page explains what Prisma recorded, how to find the migration and the reason, the two ways out with `prisma migrate resolve`, and why `migrate deploy` belongs before the start and not in the build. It ends with how migrations run on Livops.

## What does P3009 mean?

Prisma keeps every migration it has run in the `_prisma_migrations` table. `migrate deploy` writes a row when a migration starts and marks it finished when the SQL succeeds; when the SQL fails, it stores the database's error in that row's logs[4]. Before applying anything, `migrate deploy` looks for rows that have no `finished_at` and no `rolled_back_at`. If it finds one, it stops with P3009 and applies nothing[4][1][2].

So P3009 is not the original failure. The run where the migration broke usually printed P3018, which carries the real database error[3]:

```text
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: 20261201091400_add_user_email

Database error code: ...

Database error:
...
```

When the failed row has logs, P3009 repeats them after `failed with the following logs:`[4]. That is where to look first.

| Code | Message (shortened) | When you see it |
| --- | --- | --- |
| `P3018` | A migration failed to apply | The run where the SQL failed[3] |
| `P3009` | migrate found failed migrations in the target database | Every `migrate deploy` after that[3] |
| `P3008` | The migration … is already recorded as applied in the database | `resolve --applied` on a migration that is already applied[3] |
| `P3011` | Migration … cannot be rolled back because it was never applied to the database | `resolve --rolled-back` with a wrong or shortened name[3] |
| `P3012` | Migration … cannot be rolled back because it is not in a failed state | `resolve --rolled-back` on a migration that did not fail[3] |

## Why a migration fails

Prisma's docs list the usual reasons[5]:

- **The SQL has an error**, often after the migration file was edited by hand before it ran.
- **A required (`NOT NULL`) column was added to a table that already has rows.** It works on an empty development database and fails on production data.
- **The migration process stopped unexpectedly**, for example a build or a start that was cancelled or killed halfway.
- **The database shut down during the migration.**

After a deploy, add one more: the object already exists. A table or column created earlier by `prisma db push` or by hand makes the migration that creates it fail on production.

## How to fix P3009

### 1. Find the migration and the reason

The P3009 message names the migration. `prisma migrate status` compares the migrations folder with `_prisma_migrations` and exits with code 1 when failed migrations are found[6]:

```sh
npx prisma migrate status
```

Each row in `_prisma_migrations` keeps the error in its `logs` column[5]. To see the failed rows directly:

```sql
SELECT migration_name, started_at, logs
FROM _prisma_migrations
WHERE finished_at IS NULL AND rolled_back_at IS NULL;
```

### 2. Check what the failed migration left behind

A migration can be partly executed: in Prisma's own example, the first two steps succeed and the third fails[5]. Compare the migration's `migration.sql` with the database. Which of its tables, columns and indexes already exist? That decides the next step. Take a backup before you change anything.

### 3a. Roll back, fix, deploy again

Use this when the migration should run again, usually because its SQL needs a fix. Prisma's steps[5]:

1. Remove what the failed attempt created, or make the migration safe to run twice, for example `CREATE TABLE ... IF NOT EXISTS`.
2. Fix the cause in `migration.sql`. For the `NOT NULL` case, add the column with a default, or add it nullable, fill it, then make it required.
3. Mark the migration as rolled back, and deploy:

```sh
npx prisma migrate resolve --rolled-back "20261201091400_add_user_email"
npx prisma migrate deploy
```

`--rolled-back` only updates the record in `_prisma_migrations`[5]. `migrate deploy` then treats a migration whose only record is rolled back as not applied, and runs it again[4]. Use the full folder name: a shortened name gives P3011[3].

### 3b. Finish it by hand, mark it applied

Use this when the database should end up exactly as the migration describes and it is easier to finish the remaining steps yourself. Run them on the production database, then record the migration without running its SQL[5]:

```sh
npx prisma migrate resolve --applied "20261201091400_add_user_email"
```

`migrate resolve` works only on a failed migration; on a successful one it gives an error[6]. You must pass either `--rolled-back` or `--applied`[6].

### Undo or finish with migrate diff

On Prisma 7 you can also let Prisma write the SQL. To undo a partly applied migration, diff the database against the migrations folder, run the result, and mark it rolled back[5]:

```sh
npx prisma migrate diff \
  --from-config-datasource \
  --to-migrations ./prisma/migrations \
  --config prisma.config.prod.ts \
  --script > backward.sql

npx prisma db execute --config prisma.config.prod.ts --file backward.sql
npx prisma migrate resolve --rolled-back "20261201091400_add_user_email"
```

To finish it instead, diff the database against the schema with `--to-schema`, run that script, and `resolve --applied`[5]. `db execute` runs the script without touching the migrations table[5]. In Prisma 7 `db execute` no longer takes `--url`, so the production database is named in a separate config file, `prisma.config.prod.ts` in these commands[5].

## Run migrate deploy before the start, not in the build

`migrate deploy` applies pending migrations and is meant for production and test environments[6][7]. It does not reset the database or detect drift[7]. Prisma says it should generally run as part of an automated CI/CD pipeline, and does not recommend running it locally against production[7][8]. Where it runs in that pipeline matters for P3009:

- **Not in the build.** A build often cannot reach the database, and a build that is cancelled or times out mid-migration is the "process stopped unexpectedly" case above. `prisma generate` needs no database and can stay in the build.
- **Not inside the start command if the start can be killed.** `prisma migrate deploy && node server.js` counts the migration against the platform's start-up check. A slow migration can make the start look failed, and a process stopped halfway leaves the record P3009 complains about.
- **Once, before the new version starts.** A release or pre-deploy step runs once per deploy, before any instance starts. Several instances running `migrate deploy` at once are serialized by an advisory lock on PostgreSQL, MySQL and SQL Server, and that lock times out after 10 seconds, which cannot be configured[7].
- **Keep the `prisma` package installed where it runs.** Platforms that prune dev dependencies remove the CLI; Prisma suggests moving `prisma` to `dependencies` then[8].

Test the migration on a copy of production-like data first. A `NOT NULL` column that fails on production passes on an empty staging database.

## On Livops

- **Pre-deploy command.** In the service's settings, the **Pre-deploy command** runs once before each new version starts, such as migrations: `npx prisma migrate deploy`. Static sites do not have one.
- **A failed migration keeps the old version.** If the pre-deploy command ends with an error, the deploy stops as `predeploy_failed`: the new version is not started, the old one keeps serving, and the command's output is in the log. P3018 the first time, P3009 after that.
- **The build has no database.** Keep `prisma migrate deploy` out of the build command. `prisma generate` is fine there: when the project has `prisma` and `prisma/schema.prisma`, Livops runs `prisma generate` before the build itself.
- **The start command has a minute.** A start that does not answer within a minute fails as `start_failed`, another reason to keep migrations out of it.
- **Back up before you resolve.** On the database page you can take a backup of the current state before risky work. A restore never overwrites a database: it opens as a new database next to the original.
- **Running migrate resolve.** The database has no public port. From your computer, open it with **Connect from your computer**, run the SSH tunnel command it shows, and point `DATABASE_URL` at `localhost` and the tunnel's port while you run `migrate status` and `migrate resolve`. Or put the resolve in front of the migration for one deploy — `npx prisma migrate resolve --rolled-back "<name>" && npx prisma migrate deploy` — and set the pre-deploy command back afterwards, since on the next deploy the migration is no longer failed and `resolve` stops with P3012.
- **Staging first.** A staging environment gets its own empty database of each type production has, so a migration can fail there before it reaches production; it cannot catch a failure that only production's data causes.

A web app costs **39 credits a month**. The pre-deploy failure is described in [The app did not start](https://livops.dev/en/docs/error-start#predeploy), the database variables and backups in [Databases](https://livops.dev/en/docs/databases), and a database the migration cannot reach in [Prisma P1001: Can't reach database server](https://livops.dev/en/guides/error-prisma-p1001-cant-reach-database).

## Questions

**What does P3009 mean in Prisma?**

That the _prisma_migrations table has a migration that started and never finished, and was not marked as rolled back. Prisma refuses to apply new migrations until you resolve it. The migration failed on an earlier run; P3009 is what every run after that one prints.

**Should I use migrate resolve --rolled-back or --applied?**

--rolled-back when the migration's changes are not in the database, or you have removed them, and you want migrate deploy to run it again, usually after fixing its SQL. --applied when you have finished its steps by hand and the database already matches the migration: it records the migration as applied without running its SQL.

**Does migrate resolve change my tables?**

No. It only updates the migration history in _prisma_migrations. Anything the failed migration created stays until you remove it or finish it yourself, which is why you check the database before choosing --rolled-back or --applied.

**Can I just delete the row from _prisma_migrations?**

Prisma's documented way is migrate resolve, which marks the record rather than removing it. Deleting history rows by hand is outside the documented workflow, and it does nothing about the half-applied changes either.

**Why does it keep failing after I marked it rolled back?**

Because migrate deploy runs the same migration again, and its SQL fails the same way. Fix the SQL first, or remove what the earlier attempt left behind, for example a table that was already created, then resolve and deploy.

**Should prisma migrate deploy run in the build?**

No. The build often cannot reach the database, and a build that is stopped halfway can leave exactly this failed record. Run migrate deploy once, as a step before the new version starts, against the production database.

**Does P3009 happen with MongoDB?**

No. Prisma Migrate, migrate deploy and migrate resolve included, does not support MongoDB; db push is used there instead.

## Sources

1. [Prisma ORM v7 — Error reference](https://www.prisma.io/docs/orm/v7/reference/error-reference). Checked 3 December 2026.
2. [Prisma ORM v6 — Error message reference](https://www.prisma.io/docs/orm/v6/reference/error-reference). Checked 3 December 2026.
3. [prisma-engines source — user-facing-errors, schema_engine.rs](https://github.com/prisma/prisma-engines/blob/main/libs/user-facing-errors/src/schema_engine.rs). Checked 3 December 2026.
4. [prisma-engines source — apply_migrations.rs](https://github.com/prisma/prisma-engines/blob/main/schema-engine/commands/src/commands/apply_migrations.rs). Checked 3 December 2026.
5. [Prisma ORM v7 — Patching & hotfixing](https://www.prisma.io/docs/orm/v7/prisma-migrate/workflows/patching-and-hotfixing). Checked 3 December 2026.
6. [Prisma ORM v7 — Prisma CLI reference](https://www.prisma.io/docs/orm/v7/reference/prisma-cli-reference). Checked 3 December 2026.
7. [Prisma ORM v7 — Development and production](https://www.prisma.io/docs/orm/v7/prisma-migrate/workflows/development-and-production). Checked 3 December 2026.
8. [Prisma ORM v7 — Deploying database changes with Prisma Migrate](https://www.prisma.io/docs/orm/v7/prisma-client/deployment/deploy-database-changes-with-prisma-migrate). Checked 3 December 2026.
