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.
query failed: CREATE TABLE "users" (...)
error: error: relation "users" already exists
QueryFailedError: relation "users" already existsCommon 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.
export const AppDataSource = new DataSource({
synchronize: false,
migrations: ['src/migrations/*.ts'],
});Run migrations against an empty database
- Use a fresh service container or drop/recreate the schema before migrating.
- Ensure no seed step pre-creates the tables.
- Re-run
migration:runon the clean database.
How to prevent it
- Never combine
synchronize: truewith migrations in CI. - Start each CI run from a clean database.
- Let migrations own schema creation end to end.