skip to content

After two feature branches each add an Alembic revision on the same parent, why does `alembic upgrade head` fail, and how do you fix it?

level: seniorimportance: should knowfreq 42%

answer

  1. two children, one parent
  2. head is now ambiguous
  3. alembic heads prints two lines
  4. a revision with two parents

basics

~20 s

Both revisions name the same down_revision, so the chain forks into two heads and the target head no longer means one revision. alembic merge heads writes an empty revision whose down_revision lists both, giving the chain one head again.

solid answer

~50 s

Each branch ran `alembic revision` against the same parent, so after both merge, two scripts share one `down_revision` and neither is the other's parent. Alembic now has **two heads**, and `upgrade head` refuses: "Multiple head revisions are present for given argument 'head'; please specify a specific target revision, '<branchname>@head' to narrow to a specific head, or 'heads' for all heads". `alembic heads` lists both. The usual fix is `alembic merge -m "merge invoices branches" heads`, which generates a revision whose `down_revision` is a tuple of both heads and whose `upgrade()` is empty. Then `upgrade head` runs whichever branch scripts a database lacks, then the merge, and `alembic_version` collapses to one row. Alembic promises no order between the two branches, so if one needs the other's objects, re-point its `down_revision` instead, provided no shared database has run it yet.

code

console · 9 lines
console
$ alembic upgrade head
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
ERROR [alembic.util.messaging] Multiple head revisions are present for given argument 'head'; please specify a specific target revision, '<branchname>@head' to narrow to a specific head, or 'heads' for all heads
FAILED: Multiple head revisions are present for given argument 'head'; please specify a specific target revision, '<branchname>@head' to narrow to a specific head, or 'heads' for all heads
$ alembic heads
2b3c4d5e6f7a (head)
3c4d5e6f7a8b (head)
$ alembic merge -m "merge invoices branches" heads

go deeper

for a junior

Recall that a fork shows up as two lines from alembic heads and that alembic merge heads joins them.

for a middle

Explain why both scripts end up with the same down_revision and what the merge revision's tuple down_revision means.

for a senior

Show what upgrade head does on a database that already ran one branch, when re-pointing beats merging, and how a build check stops forks reaching a release.

for a principal

Set a team rule for parallel revisions: merge revisions or a linear chain, enforced by a heads check, and who resolves a fork before release.

## Why two heads appear An Alembic chain is a graph built from each script's `down_revision`. `alembic revision` sets the new script's parent to whatever is head in that working copy. Two developers starting from the same head on separate feature branches therefore both write `down_revision = "1a2b3c4d5e6f"`. Each branch is a valid chain on its own; once both land on the main line, the graph has two scripts with no child, two **heads**, and nothing in the files says which comes first. ## What Alembic says - `alembic upgrade head` fails with: "Multiple head revisions are present for given argument 'head'; please specify a specific target revision, '<branchname>@head' to narrow to a specific head, or 'heads' for all heads". - `alembic revision` refuses to pick a parent: "Multiple heads are present; please specify the head revision on which the new revision should be based, or perform a merge." - `alembic heads` prints one line per head, each marked `(head)`; `alembic history` shows the shared parent marked `(branchpoint)`. ## Fixing it with a merge revision 1. Run `alembic heads` to confirm the two identifiers. 2. Run `alembic merge -m "merge invoices branches" heads`. Alembic writes a new script whose `down_revision` is a tuple of both heads; its `upgrade()` and `downgrade()` are empty unless you add something. 3. Commit it. The chain has one head again, the merge revision. 4. Deploy with `alembic upgrade head` as usual. On each database, `upgrade head` runs whatever that database is missing. A fresh database runs the parent, both branch scripts and the merge. A database that already ran one branch, say production, which received the first feature last week, runs only the other branch's script and then the merge. The merge step deletes one `alembic_version` row and updates the other, leaving a single row. ## Re-pointing `down_revision` instead The alternative is to make the chain linear again: edit the later script so its `down_revision` names the other head. | | Merge revision | Re-point `down_revision` | |---|---|---| | History | keeps the fork, adds a node with two parents | a straight line | | Order between the branches | not defined | fixed by the edit | | Safe after a shared database ran the edited script | yes | no | Re-pointing is only safe while the edited script has not run on any database the team shares. A database that already ran it under the old parent stores it as its head; under the new graph Alembic treats the other branch's script as an ancestor already applied, so that script silently never runs there. ## Databases that already hold two heads `alembic upgrade heads` applies both branches without a merge and leaves **two rows** in `alembic_version`, one per head. That is legal, but it is a workaround: the fork remains, `upgrade head` still fails, and `alembic revision` refuses to pick a parent until the fork is merged or a head is named explicitly. ## Branches that are meant to stay apart Not every fork is an accident. A chain can carry deliberately separate lines, for example one per optional module, each starting from its own base. A script marks its line with `branch_labels`, such as `branch_labels = ("shoppingcart",)`, and commands can then target one line with `alembic upgrade shoppingcart@head`, which is the `'<branchname>@head'` form the error message suggests. Such lines are meant to keep several heads, so `upgrade heads` and labelled targets are the normal way to run them; merging is for accidental forks, where the team expects one line. ## Keeping a fork out of a deploy - Fail the build when `alembic heads` prints more than one line, so the merge happens in review rather than at release time. - Keep the two branches independent; a merge revision states that neither branch needs the other. - When one branch genuinely needs an object the other creates, re-point it, or declare `depends_on` with the other revision's identifier so Alembic applies that revision first. - Expect `alembic downgrade -1` from a merge point to fail with "Ambiguous walk"; name an explicit target revision instead.

  • In what order does `upgrade head` run the two branch scripts after the merge?
    Alembic guarantees only that a parent runs before its children, so both branch scripts run after their shared parent and before the merge. It promises no order between the two branches; in practice it can run either first. That is why a merge suits independent branches only, and a dependency calls for re-pointing `down_revision` or for `depends_on`.
  • What does `alembic downgrade -1` do when the database sits on a merge revision?
    It fails with "Ambiguous walk", because one step back from a node with two parents is not a single revision. Name an explicit target instead: downgrading to one branch's identifier runs the merge's `downgrade()` and leaves both branch heads in `alembic_version`, and `downgrade base` walks everything back.
  • What does `depends_on` add that a merge does not?
    `depends_on` names a revision that must be applied before this one without making it a parent. Alembic then runs that revision first even across branches, so a script on one branch can rely on a table another branch creates. A merge only joins two heads; it says nothing about which branch must come first.

  • Two heads mean a revision file is corrupt and must be deleted.
  • Running alembic upgrade heads in production settles the fork for good.
  • A merge revision re-runs both branches' upgrade() code on every database.
  • Alembic applies the two branches in the order their commits merged.
  • Editing down_revision is safe even after a shared database ran the script.