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.
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
- Run
prisma migrate deployafter the database is healthy. - Only then run the app or the test suite.
- For a fresh DB with committed migrations, deploy creates every table.
- run: npx prisma migrate deploy
- run: npm testPush the schema for schema-only test databases
When you do not need migration history in CI, db push syncs the schema directly before tests.
npx prisma db push --skip-generate
npm testHow 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.