Skip to content
Latchkey

TypeORM "QueryFailedError: relation does not exist" in CI

TypeORM connected and queried a table that does not exist. With synchronize: false (correct for production-like CI), the schema comes only from migrations - and they were not applied before the query. This is deterministic.

What this error means

Tests or app boot fail with "QueryFailedError: relation 'orders' does not exist". The connection works; the schema is missing because migration:run did not execute first.

psql
QueryFailedError: relation "orders" does not exist
    at PostgresQueryRunner.query (.../PostgresQueryRunner.js)

Common causes

Migrations not run before queries

With synchronize: false, only migration:run creates tables. Querying before it leaves the relation missing.

Migrations not compiled

If migrations are TypeScript and not compiled/registered, migration:run applies nothing and the schema stays empty.

Wrong database targeted

The data source points at a different database than the one migrations ran against.

How to fix it

Run migrations before tests

Terminal
npm run typeorm migration:run -- -d ./data-source.ts
npm test

Ensure migrations are discoverable

  1. Confirm the migrations glob in the data source matches your compiled output.
  2. Build TypeScript migrations before running them if needed.
  3. Keep synchronize: false and rely on migrations in CI.

How to prevent it

  • Always run migration:run before any query in CI.
  • Verify the migrations glob resolves the files you expect.
  • This is deterministic - retrying without running migrations fails identically.

Frequently asked questions

What causes ""QueryFailedError: relation does not exist""?
With synchronize: false, only migration:run creates tables. Querying before it leaves the relation missing.
How do I fix "QueryFailedError: relation does not exist"?
Run migrations before tests

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card