Sphere
Log in Join
☆ 5

alembic migration fails with duplicate column on postgres

Alembic 1.13 on Postgres 15. A migration adding a column fails on a second run: psycopg2.errors.DuplicateColumn: column "status" of relation "users" already exists I thought Alembic tracked which migrations ran via alembic_version. Why did it try to add the column twice? I'm fairly new to Alembic, so I might be missing something obvious about how the version table is supposed to work.

⚑ Report

4 replies

☆ 0
C cache32 ·2026-10-14#1

Check the alembic_version table: SELECT * FROM alembic_version;. If the revision isn't recorded, Alembic will re-run it. Common causes: the migration ran inside a transaction that rolled back after the DDL, or someone manually edited the DB, or you have two heads (alembic heads will show). Also, DDL in Postgres is transactional, so a failed migration shouldn't leave the column unless something committed outside Alembic.

⚑ Report
☆ 0
K kernel ·2026-10-16#2

alembic heads shows two heads! I must have created a branch accidentally when two people generated revisions from the same base. How do I merge them? I checked the git history and two people generated revisions from the same head within a day of each other. Neither CI nor code review flagged it because nobody ran alembic heads as part of the pipeline.

⚑ Report
☆ 0
C cache ·2026-10-23#3

Run alembic merge heads -m 'merge branches' to create a merge revision, then alembic upgrade head. Before that, reconcile the DB: if the column already exists, either stamp the revision alembic stamp <rev> to mark it applied, or make the migration idempotent with IF NOT EXISTS. Stamping is cleaner if the schema is correct.

⚑ Report
☆ 0
F ferret86 ·2026-10-27#4

Merged the two heads and stamped the revision since the column was already there. Future migrations now have a single linear history. I added a CI check for alembic heads so we catch branchy history before it hits production. YMMV depending on your workload.

⚑ Report
Log in to reply.