skip to content

A Prisma 7 app's Docker image throws PrismaClientValidationError 'Unknown field' for a column the schema already has; why is the client stale, and how should the build generate it?

level: seniorimportance: should knowfreq 30%

answer

  1. who runs generate now
  2. the output folder is source code
  3. the client embeds its schema
  4. generate needs no database

basics

~20 s

Prisma 7 never generates the client by itself: no install hook runs it, and migrate dev and db push stopped calling it. The image ships whatever the output folder held. Run prisma generate in the build, after copying the schema.

solid answer

~50 s

The Prisma 7 `prisma-client` generator writes plain TypeScript, including an inline copy of the schema, into the `output` folder the generator block requires. Nothing refreshes it for you: `@prisma/client` has no install hook that generates, and `migrate dev` and `db push` no longer run `prisma generate`. If that folder is committed or copied in from the build context, the image can carry a client generated from an older schema, and that client validates each query against its embedded schema and rejects the new field with `PrismaClientValidationError` before contacting the database. Ignore the folder in git, run `prisma generate` as a build step after copying the schema and before compiling, and let a failure stop the build. `prisma generate` needs no database, but `env("DATABASE_URL")` in `prisma.config.ts` throws when the build has no URL, so read `process.env` with a fallback there.

code

json · 8 lines
json
{
  "type": "module",
  "scripts": {
    "generate": "prisma generate",
    "build": "prisma generate && tsc",
    "release": "prisma migrate deploy"
  }
}

go deeper

for a junior

Remember that in Prisma 7 you must run prisma generate yourself whenever the schema changes, and the client lives in the output folder.

for a middle

Explain why nothing regenerates the client any more, what the generated folder contains, and how a stale one fails at runtime.

for a senior

Design the build so generation is ordered after the schema copy, the folder is never committed, and the config loads without a database URL.

for a principal

Set one build contract for every service, generate in the build and migrate at release, so stale clients cannot reach production unnoticed.

## Where the Prisma 7 client comes from `prisma generate` reads `schema.prisma` and writes **Prisma Client** for it. In Prisma 7 the default generator is `prisma-client`, and its `output` field is **required**: the client is written into a folder in your codebase, such as `generated/prisma`, rather than into `node_modules`. The output is plain TypeScript (`client.ts`, `models.ts`, `enums.ts`, a `models/` folder and an `internal/` folder), with an inline copy of the schema the client was generated from. There is no native query-engine binary in it; `binaryTargets` was a setting of the deprecated `prisma-client-js` generator, which shipped such binaries. The consequence: the generated folder is build output that looks like source code, and it is only as current as the last `prisma generate`. ## Why an image ends up stale Nothing in Prisma 7 regenerates the client behind your back: - `@prisma/client` has no install script that runs `prisma generate`, so `npm ci` in a Dockerfile installs packages and nothing more; - `prisma migrate dev` and `prisma db push` no longer run `prisma generate` (their `--skip-generate` flag was removed with the behaviour); - the folder is wherever `output` says, so a `COPY . .` brings in whatever copy sits in the build context, and a committed folder brings in whatever was committed. So the image contains a client generated from some earlier schema unless the build regenerates it. ## What a stale client does | Schema change since the client was generated | Symptom | |---|---| | New field used in `select` or `where` | `PrismaClientValidationError`: "Unknown field" or "Unknown argument", raised before any database call | | New model | `prisma.post` is `undefined`, so `prisma.post.findMany` throws a `TypeError` | | Any change, with type checking in the build | `tsc` fails on the new names, if it compiles against the same stale folder | The validation error is the giveaway: the client checks each query against its embedded schema, so it rejects a field the database already has. ## Generating in the build Make generation an explicit, ordered build step: 1. Add the generated folder to `.gitignore` (and the Docker ignore file) so no stale copy can be committed or copied in. 2. Install dependencies, including the `prisma` CLI, in the build stage. 3. Copy `schema.prisma` and `prisma.config.ts` into the image. 4. `RUN npx prisma generate`, so the folder is written from the schema in this commit. 5. Compile or bundle the application, which now imports the fresh client. 6. In a multi-stage build, copy the generated folder (or the compiled output that includes it) into the runtime stage. A `"build": "prisma generate && tsc"` script keeps the order in one place for local builds, CI and the image alike. ## The config file at build time `prisma generate` does not connect to the database, but the CLI loads `prisma.config.ts` before running it. If that file uses `env("DATABASE_URL")` and the build stage has no such variable, loading fails with `PrismaConfigEnvError: Cannot resolve environment variable: DATABASE_URL.`, and the build stops before generating anything. Either provide the variable in the build or read `process.env.DATABASE_URL ?? ""` in the config; a later command that does need a database then fails on its own with "Connection url is empty". ## Generate is not migrate Generating the client and changing the database are separate steps. `prisma generate` belongs in the build and needs no credentials. `prisma migrate deploy` applies pending migrations and needs a real `datasource.url`, so it runs at release time against the target database. Running migrations does not regenerate the client in Prisma 7, so a pipeline that only migrates still ships a stale client. ## Catching it before production Two cheap checks stop a stale client from shipping: - In CI, run `prisma generate` and then the type checker. Code that uses a new field compiles only against a client generated from the new schema, so a missing generate step fails here instead of in production. - If a generated folder is committed despite the advice, add a CI step that regenerates and fails when the working tree changes; that proves the committed copy matches the schema. Both checks treat the schema as the source and the generated client as an artifact derived from it, which is the stance Prisma 7's explicit `output` path invites.

  • What changes when the generated client has to run in an edge runtime?
    The `prisma-client` generator's `runtime` field (default `nodejs`) selects the target the generated code is built for, so the build must generate for that target. The client still needs a driver adapter whose driver works there: many edge runtimes cannot open ordinary TCP sockets, so you pick an HTTP or WebSocket based driver and its adapter. The v7 docs still label deployment to the main edge platforms as Preview.
  • Why not commit the generated folder so the build does not need the Prisma CLI?
    Then correctness depends on every developer running `prisma generate` before each commit, and any schema change committed without it ships a mismatched client. Generating in the build ties the client to the schema in the same commit, at the cost of keeping the `prisma` CLI available in the build stage.

  • npm install regenerates the Prisma 7 client automatically through a postinstall hook.
  • Running prisma migrate dev also regenerates the client in Prisma 7.
  • prisma generate needs a reachable database, so it must run at container start.
  • A stale client only matters for TypeScript types; queries still work at runtime.
  • Running prisma migrate deploy in the pipeline keeps the client current.

Assumes Prisma 7, where the prisma-client generator needs an explicit output path and migrate dev and db push no longer run prisma generate.