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.
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
npm run typeorm migration:run -- -d ./data-source.ts
npm testEnsure migrations are discoverable
- Confirm the
migrationsglob in the data source matches your compiled output. - Build TypeScript migrations before running them if needed.
- Keep
synchronize: falseand rely on migrations in CI.
How to prevent it
- Always run
migration:runbefore any query in CI. - Verify the migrations glob resolves the files you expect.
- This is deterministic - retrying without running migrations fails identically.