Skip to content

How to Fix Python Alembic Migration Head Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Python Alembic Migration Head Error. We cover key concepts, practical examples, and best practices.

Alembic raises errors when migration branches diverge, the database head does not match the latest migration, or there are conflicting revision dependencies.

The Wrong Way

alembic upgrade head

Output:

alembic.util.exc.CommandError: Multiple head revisions: abc123 (head), def456 (head)

The Right Way

# Check the current state
alembic current

# See all heads
alembic heads

# Merge the two heads into one
alembic merge heads -m "merge heads"

# Now upgrade
alembic upgrade head

Output:

INFO  [alembic.runtime.migration] Running merge abc123, def456 -> merged

Prevention Tips

  • Run alembic upgrade head before creating new migrations.
  • Use alembic check to detect multiple heads before applying.
  • Avoid creating migrations from different branches simultaneously.
  • Use alembic merge to resolve divergent heads.
  • Use revision IDs consistently with depends_on.

Common Mistakes with alembic migration

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why do I get "Multiple head revisions"?

Two developers created migrations from the same base revision. Alembic does not know which one to apply first. Use alembic merge to create a combined migration that depends on both.

How do I see the migration history?

Use alembic history to show all migrations in order. Use alembic history --verbose for detailed information. This helps identify where branches diverge.

What is the difference between upgrade and downgrade?

Upgrade moves the database forward to a newer revision. Downgrade reverts to an older revision. Always test both directions before deploying. Use alembic downgrade -1 to revert one step.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro