Skip to main content

prisma generate does not run migrations

8 min read
Prisma
PostgreSQL
Vercel
CI/CD
Database
SC
Written by Shailesh Chaudhari
Full-stack engineer with a backend focus

prisma generate rebuilds the Prisma client from schema.prisma. It never connects to a database. If your Vercel build runs prisma generate and nothing else, your production schema only changes when a human remembers to run prisma migrate deploy by hand, and the first time they forget, the code deploys against tables it expects and the database does not have. The fix is to run prisma migrate deploy in the build, on production deploys only, and to check the _prisma_migrations table before you automate it.

I shipped this bug twice, in two apps, and the second app taught me the part the first one didn't.

What broke

KhataGO is a Next.js app. EduScale's backend is an Express service. Both run on Vercel with Prisma over Postgres, and both had "postinstall": "prisma generate" in package.json. KhataGO's build was prisma generate && next build. EduScale's backend build was tsc && tsc-alias.

Everything in that list is a compile step. The Prisma CLI reads schema.prisma and writes TypeScript types and a query engine into node_modules. The code that comes out of the build knows about every column in the schema. The database has no idea any of this happened.

So a pull request that added a column to the schema, with a migration file beside it, would build green, deploy, and then fail on the first query that touched the new column. In KhataGO that looked like a column error at runtime (KhataGO/FINDINGS.md, section 3). In EduScale it was quieter and worse.

How I found it

On EduScale I ran a read-only prisma migrate status against production, which only reads the _prisma_migrations table. It listed four migrations the database had never applied:

20260622000000_add_ai_code_review_fields
20260622010000_add_content_embedding
20260622020000_add_user_rating
20260823120000_unique_ai_review_per_submission

Every one of them backed code that was already merged and deployed. Nobody had noticed because the AI features those migrations serve were switched off: GEMINI_API_KEY was unset in production, so the code paths that would have read ContentEmbedding never ran. The missing key was hiding the missing schema.

That is not as comforting as it sounds. The rating and matchmaking endpoints need no LLM at all, and the UserRating table did not exist in production. Those endpoints failed every time they were called. And setting the API key would not have turned the AI features on. It would have turned them on against a database with no ContentEmbedding table (EduScale/FINDINGS.md, section 7).

Check the migration table before you automate anything

On KhataGO, before I made every production deploy run migrations, I looked at what production's _prisma_migrations table actually held. It had two applied migrations that do not exist in the repository. Someone had run DDL against production by a route the repo does not record.

That matters because migrate deploy compares the table to the migrations directory. If it had refused to run against that drift, my fix would have broken every deploy, which is worse than the problem it was fixing. So I reproduced the drift on a throwaway database and ran the command there:

drift only                        ->  No pending migrations to apply.  EXIT=0
drift + one pending migration     ->  applied successfully, columns present

Only then did it go into the build (KhataGO/FINDINGS.md, section 3). "This command is safe" is a hypothesis until you run it against the shape your production database actually has.

One aside. If you do this on a repo where .env points at production, exporting DATABASE_URL in your shell is not enough to redirect the Prisma CLI. Both schemas declare directUrl = env("DIRECT_URL"), migrations use it, and the only place DIRECT_URL is set is .env, which EduScale's prisma.config.ts loads explicitly. I wrote that one up separately in the test suite that wrote to production. docs/MIGRATIONS.md has the full recipe: work from a directory with no .env, copy the schema and migrations there, pass --schema, then read the Datasource "db" line the CLI prints before anything else.

The fix

Both repos now have scripts/vercel-build.sh as their build command. The EduScale one, in full apart from its comment block:

set -euo pipefail

if [ "${VERCEL_ENV:-}" = "production" ]; then
  echo "▶ production deploy — applying migrations"
  npx prisma migrate deploy
else
  echo "▶ ${VERCEL_ENV:-local} build — skipping migrations (production only)"
fi

npx tsc
npx tsc-alias --resolve-full-paths

KhataGO's is the same shape with prisma generate and next build at the bottom.

Two decisions in there are deliberate.

Production only. On Vercel, a variable added with the default settings applies to Preview as well as Production, so a preview build can see the production DATABASE_URL. Migrating unconditionally would let a feature branch that may never merge change production's schema. migrate deploy is additive and never resets, so the damage would be small, but small is not a reason to hand that decision to a branch. Previews build against whatever schema production already has, which is what a preview should be testing.

A failed migration fails the build. That is the point of set -e. A deploy that cannot migrate must not go live and start serving errors from tables it does not have. The site keeps serving the last good build instead.

EduScale's CI runs the same prisma migrate deploy against a throwaway Postgres service container before the backend tests (.github/workflows/ci.yml, backend-test). That proves the chain applies to an empty database. It cannot prove anything about a database that already holds objects, and that gap is the second half of this story.

The part I got wrong

Failing the build on a bad migration is the right design. It is also the whole blast radius. Anything that makes migrate deploy refuse to run stops every deploy, including the ones that have nothing to do with the bad migration.

Six weeks after the KhataGO fix, a migration added six indexes with plain CREATE INDEX. Three of them already existed in production, almost certainly created by those two unaccounted-for migrations. Postgres raised 42P07 relation ... already exists on the first statement, rolled the whole migration back, and Prisma wrote the failure into _prisma_migrations. From then on every production build died at migrate deploy with P3009 before compiling anything. The site kept serving the last good deploy, so nothing looked broken from outside. Production did not deploy for a week (KhataGO/FINDINGS.md, section 10).

The migration had been tested against a local database. That database was built by replaying the repo's own migrations, so it did not have the three indexes. It passed every check I ran, because the local database and production disagreed about exactly the thing the migration asserted.

The rule I took from it: a migration's job is to make the database match the schema afterwards, not to assert what the database did not have beforehand. CREATE INDEX IF NOT EXISTS says the first. A bare CREATE INDEX quietly says the second, inside a transaction that takes every other statement down with it.

EduScale now enforces this in Backend/src/tests/migrations/migrationIdempotency.test.ts. The test parses every migration file and fails any new one that creates an index, table, extension or column without IF NOT EXISTS, or a type or constraint without a duplicate_object handler or a matching DROP ... IF EXISTS earlier in the file. Migrations production has already applied are pinned by sha256 and counted in a ratchet map that can only go down, because editing an applied file is its own way to stop every deploy (P3006). When I measured on 2026-09-06, production's table held exactly the fifteen migrations then in the repo, all checksums matched, and a replay onto an empty Postgres 17 produced the same object set as production. That is a fact with a date on it, not a property of the code, which is why the test exists.

What to do in your own repo

Run npx prisma migrate status against production, read-only, and read the datasource line it prints. If it lists pending migrations your code already depends on, you have this bug.

Query _prisma_migrations directly and compare it to your migrations directory. Extra rows mean DDL reaches your database by routes the repo does not record. Test how migrate deploy behaves against that drift on a scratch database before it goes in the build.

Put prisma migrate deploy in the build, gated on VERCEL_ENV=production, and let a failure fail the build.

Write every object creation in a form that tolerates the object already existing. IF NOT EXISTS where Postgres offers it, a DO ... EXCEPTION WHEN duplicate_object block where it does not. Never edit a migration production has applied.

And when a deploy does wedge, migrate status will not tell you. Reproduced on EduScale's own chain, it listed only the migration queued behind the failed one as pending. It never named the failed one and never used the word "failed". The reliable check is SELECT migration_name FROM _prisma_migrations WHERE finished_at IS NULL. Recovery is migrate resolve --rolled-back <name> after fixing the SQL, then migrate deploy. docs/MIGRATIONS.md in the EduScale repo walks through it.

SC
Written by Shailesh Chaudhari
Full-stack engineer with a backend focus