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

时间:2016-05-04 11:51:48

标签: python sqlalchemy migration

我定义了流动的模型。

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',得到了流动的迁移文件:

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

然后,我删除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:最新的verison
  sqlalchemy:1.0
  框架:猎鹰
  系统:Debian GNU / Linux 7

2 个答案:

答案 0 :(得分:0)

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

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

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

.
├── __init__.py
├── alembic
│   ├── README
│   ├── env.py
│   ├── script.py.mako
│   └── versions
├── alembic.ini
├── app
│   ├── __init__.py
│   └── models.py
└── requirements.txt

3 directories, 9 files

我在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

之后,我运行了自动生成命令:

$ 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

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

$ 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

如您所见,我的第二次修订中的评论“第二次”是故意的,它帮助我更准确地准确地记录了name列的移除过程。我认为当您尝试确定迁移历史时,它会更清晰。

希望有所帮助!

答案 1 :(得分:0)

哦,最后我发现了问题。我的sys.path指向错误的目录,其中是我的代码的旧版本。因此,每次运行alembic revision --autogenerate时,它都会从未修改的旧代码中读取。