alembic for sqlalchemy不检测列的添加和删除

2024-05-23 14:03:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我定义了流动模型。在

class JjfqServiceProvidorRecord(Base):
    __tablename__ = 'jjfq_service_providor_record'
    id = Column(Integer, primary_key=True)
    phone = Column(String(16))
    name = Column(String(20))

然后我运行alembic revision --autogenerate -m 'first',得到了流动的迁移文件:

^{pr2}$

然后,我删除name字段,如下所示:

class JjfqServiceProvidorRecord(Base):
    __tablename__ = 'jjfq_service_providor_record'
    id = Column(Integer, primary_key=True)
    phone = Column(String(16))
    name = Column(String(20))

然后我重新运行alembic revision --autogenerate -m 'second',这个命令生成的迁移文件是:

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    pass
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    pass
    ### end Alembic commands ###

怎么了?为什么alembic没有检测到列的添加和删除?在

python : Python 2.7.5
alembic : latest verison
sqlalchemy: 1.0
framework : falcon
system : Debian GNU/Linux 7


Tags: nameidbasestringservicecolumnintegeralembic
2条回答

首先,我将描述我的环境:

OS X El Capitan
MySQL Server 5.7
alembic (0.8.6)
PyMySQL (0.7.2)
SQLAlchemy (1.0.12)

这是我在安装和初始化Alembic之后的目录结构:

^{pr2}$

我已经在app/models.py中定义了模型:

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))

对于alembic/env.py,我有以下内容:

from app import *
from app.models import Base
target_metadata = Base.metadata

之后,我运行了autogeneration命令:

$ 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

第一个迁移文件应该如下所示:

"""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 ###

如果您对第一个迁移文件的外观满意,请运行升级迁移:

$ 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

之后,您可以从app/models.py中的表定义中删除jjfq_service_providor_record.name列,并运行第二个自动生成:

$ 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

第二个迁移文件应该如下所示:

"""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 ###

如果这看起来也不错,运行第二次迁移,您应该已经准备好了!在

$ 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

如您所见,我第二次修订中的注释“second”是经过深思熟虑的,它帮助我更准确地列出删除name列的确切位置。我认为当你试图确定迁移历史时,它会让你更清楚。在

希望能帮上忙!在

哦,我终于发现了问题。我的系统路径指向错误的目录,其中是我代码的旧版本。所以每次我运行alembic revision autogenerate,它都会从未修改的旧代码中读取。在

相关问题 更多 >