<p>首先,我将描述我的环境:</p>
<pre><code>OS X El Capitan
MySQL Server 5.7
alembic (0.8.6)
PyMySQL (0.7.2)
SQLAlchemy (1.0.12)
</code></pre>
<p>这是我在安装和初始化Alembic之后的目录结构:</p>
^{pr2}$
<p>我已经在<code>app/models.py</code>中定义了模型:</p>
<pre><code>from sqlalchemy import Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class JjfqServiceProvidorRecord(Base):
__tablename__ = 'jjfq_service_providor_record'
id = Column(Integer, primary_key=True)
phone = Column(String(16))
name = Column(String(20))
</code></pre>
<p>对于<code>alembic/env.py</code>,我有以下内容:</p>
<pre><code>from app import *
from app.models import Base
target_metadata = Base.metadata
</code></pre>
<p>之后,我运行了autogeneration命令:</p>
<pre><code>$ alembic revision autogenerate -m 'first'
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.autogenerate.compare] Detected added table 'jjfq_service_providor_record'
Generating /tmp/37027017/alembic/versions/5b157ced3fa0_first.py ... done
</code></pre>
<p>第一个迁移文件应该如下所示:</p>
<pre><code>"""first
Revision ID: 5b157ced3fa0
Revises:
Create Date: 2016-05-05 21:53:57.687433
"""
# revision identifiers, used by Alembic.
revision = '5b157ced3fa0'
down_revision = None
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('jjfq_service_providor_record',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('phone', sa.String(length=16), nullable=True),
sa.Column('name', sa.String(length=20), nullable=True),
sa.PrimaryKeyConstraint('id')
)
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_table('jjfq_service_providor_record')
### end Alembic commands ###
</code></pre>
<p>如果您对第一个迁移文件的外观满意,请运行升级迁移:</p>
<pre><code>$ alembic upgrade head
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> 5b157ced3fa0, first
</code></pre>
<p>之后,您可以从<code>app/models.py</code>中的表定义中删除<code>jjfq_service_providor_record.name</code>列,并运行第二个自动生成:</p>
<pre><code>$ alembic revision autogenerate -m 'second'
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.autogenerate.compare] Detected removed column 'jjfq_service_providor_record.name'
Generating /tmp/37027017/alembic/versions/acbba548b6cd_second.py ... done
</code></pre>
<p>第二个迁移文件应该如下所示:</p>
<pre><code>"""second
Revision ID: acbba548b6cd
Revises: 5b157ced3fa0
Create Date: 2016-05-05 21:55:23.130404
"""
# revision identifiers, used by Alembic.
revision = 'acbba548b6cd'
down_revision = '5b157ced3fa0'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_column('jjfq_service_providor_record', 'name')
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('jjfq_service_providor_record', sa.Column('name', mysql.VARCHAR(length=20), nullable=True))
### end Alembic commands ###
</code></pre>
<p>如果这看起来也不错,运行第二次迁移,您应该已经准备好了!在</p>
<pre><code>$ alembic upgrade head
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.runtime.migration] Running upgrade 5b157ced3fa0 -> acbba548b6cd, second
</code></pre>
<p>如您所见,我第二次修订中的注释“second”是经过深思熟虑的,它帮助我更准确地列出删除<code>name</code>列的确切位置。我认为当你试图确定迁移历史时,它会让你更清楚。在</p>
<p>希望能帮上忙!在</p>