Skip to content
Latchkey

TypeORM QueryFailedError "relation already exists" in CI

TypeORM ran a migration statement and the database rejected it because the object already exists. The most common cause in CI is synchronize: true creating the schema, then a migration trying to create the same tables.

What this error means

typeorm migration:run fails with "QueryFailedError: relation \"users\" already exists" (or "table ... already exists" on MySQL) while applying a CREATE TABLE migration.

typeorm output
query failed: CREATE TABLE "users" (...)
error: error: relation "users" already exists
QueryFailedError: relation "users" already exists

Common causes

synchronize is creating the schema before migrations

With synchronize: true, TypeORM builds the schema from entities at startup; the migration then tries to create the same tables and the database refuses.

The database was not reset between runs

A reused database already contains the tables, so a fresh migration run collides with existing objects.

How to fix it

Turn off synchronize and rely on migrations

Disable synchronize so migrations are the only thing that builds the schema, avoiding the double create.

data-source.ts
export const AppDataSource = new DataSource({
  synchronize: false,
  migrations: ['src/migrations/*.ts'],
});

Run migrations against an empty database

  1. Use a fresh service container or drop/recreate the schema before migrating.
  2. Ensure no seed step pre-creates the tables.
  3. Re-run migration:run on the clean database.

How to prevent it

  • Never combine synchronize: true with migrations in CI.
  • Start each CI run from a clean database.
  • Let migrations own schema creation end to end.

Related guides

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