Skip to content
Latchkey

Prisma "relation does not exist" (migrate before querying) in CI

Prisma passed a query to a database whose schema has no such table. In CI the almost universal cause is that migrations were not applied before the app or the test suite started querying.

What this error means

A query fails with "The table public.User does not exist in the current database." or Postgres "relation \"User\" does not exist", right after a clean database was started.

Prisma
PrismaClientKnownRequestError:
Invalid `prisma.user.findMany()` invocation:
The table `public.User` does not exist in the current database. (P2021)

Common causes

Migrations never ran against the CI database

The fresh service database is empty; without a migrate step, the tables the queries expect do not exist.

Tests run before the migrate step finishes

A missing dependency ordering lets tests start before migrate deploy has created the schema.

How to fix it

Apply migrations before querying

  1. Run prisma migrate deploy after the database is healthy.
  2. Only then run the app or the test suite.
  3. For a fresh DB with committed migrations, deploy creates every table.
.github/workflows/ci.yml
- run: npx prisma migrate deploy
- run: npm test

Push the schema for schema-only test databases

When you do not need migration history in CI, db push syncs the schema directly before tests.

Terminal
npx prisma db push --skip-generate
npm test

How to prevent it

  • Always create the schema (deploy or push) before querying in CI.
  • Order steps so migrations complete before tests start.
  • Use a fresh database and apply committed migrations to it.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →