Skip to content
Latchkey

Sequelize "sequelize db:migrate" Fails in CI

sequelize-cli ran your migrations and one of two things went wrong: it could not connect to the database, or a migration’s up threw. The error text distinguishes a connectivity problem from a migration-content problem.

What this error means

npx sequelize-cli db:migrate fails either with a connection error (refused/access denied) before anything runs, or with a thrown error from inside a specific migration. The two need different fixes.

sequelize-cli output
ERROR: connect ECONNREFUSED 127.0.0.1:5432
# or
== 20240115-add-status: migrating =======
ERROR: column "status" of relation "orders" already exists

Common causes

Cannot connect - wrong config/env for this NODE_ENV

sequelize-cli reads config/config.js keyed by NODE_ENV. If the CI environment’s entry has the wrong host/port/credentials (or the database is not ready), the connection fails first.

A migration’s up() throws

A duplicate column, missing table, or constraint violation inside a migration makes db:migrate fail at that step. It is deterministic and points at the specific migration.

SequelizeMeta disagrees with the schema

If SequelizeMeta thinks a migration has not run but its objects exist (or vice versa), the next run re-applies or skips incorrectly and errors.

How to fix it

For connection errors, fix config and readiness

Make sure the config entry for the CI NODE_ENV matches the reachable database, and wait for it to be ready.

Terminal
export NODE_ENV=test
until pg_isready -h "$DB_HOST" -p "$DB_PORT"; do sleep 1; done
npx sequelize-cli db:migrate

For a throwing migration, fix the SQL and run clean

  1. Read which migration threw and why (already-exists vs missing object).
  2. Run migrations against a fresh CI database so each up executes once.
  3. Correct the migration if it genuinely conflicts with a prior one.

Reconcile SequelizeMeta

On a non-clean database, align the meta table with reality before continuing.

Terminal
npx sequelize-cli db:migrate:status   # see applied vs pending

How to prevent it

  • Set NODE_ENV and keep the matching config entry aligned with the CI database.
  • Run migrations against an ephemeral database so each up runs once.
  • Use db:migrate:status in CI to catch meta/schema drift.

Frequently asked questions

What causes ""ERROR: ... db:migrate""?
sequelize-cli reads config/config.js keyed by NODE_ENV. If the CI environment’s entry has the wrong host/port/credentials (or the database is not ready), the connection fails first.
How do I fix "ERROR: ... db:migrate"?
Make sure the config entry for the CI NODE_ENV matches the reachable database, and wait for it to be ready.

Related guides

References

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