<p>也许最传统的(和健壮的)解决方案是使用<code>alembic merge heads</code>。以同样的方式,当您在Git中有两个分支时,可以通过合并提交将它们重新组合在一起;在Alembic中,当您有两个分支时,可以通过合并修订将它们重新组合在一起。</p>
<p>例如,假设我们有一个添加表a的修订版1a6b1a4a0574和一个添加表B的修订版2e49118db057</p>
<pre class="lang-none prettyprint-override"><code>$ alembic history
<base> -> 2e49118db057 (head), Add table B
<base> -> 1a6b1a4a0574 (head), Add table A
</code></pre>
<p>然后我们可以通过运行<code>alembic merge heads</code>来合并它们:</p>
<pre class="lang-none prettyprint-override"><code>$ alembic merge heads
Generating /Users/markamery/alembictest/alembic/versions/409782f4c459_.py ... done
$ alembic history
2e49118db057, 1a6b1a4a0574 -> 409782f4c459 (head) (mergepoint), empty message
<base> -> 2e49118db057, Add table B
<base> -> 1a6b1a4a0574, Add table A
</code></pre>
<p>如果您的某个修订可能已经在某个地方运行(包括在您的一个同事的开发机器上),那么您可能希望使用<code>alembic merge</code>,而不是像这里的其他答案所建议的那样,修改其中一个修订的<code>down_revision</code>。修改向下修订的危险在于,它可能导致修订永远无法应用。例如,假设您的同事Bob已经用2e49118db057版本拉下了您的分支,并运行<code>alembic upgrade head</code>,创建了表B,然后您决定修改2e49118db057的<code>down_revision</code>,以指向1A6B1A4 a0574,这是Bob从未见过或运行过的。鲍勃放下你的零钱,跑<code>alembic upgrade head</code>,然后。。。什么也没发生,因为就Alembic而言,他已经在<code>head</code>了,不需要运行1a6b1a4a0574。因此,Bob最终永远得不到表A,也可能永远不知道为什么他的数据库处于崩溃状态。</p>
<p>不要破坏Bob的数据库-改为进行合并修订。</p>