Python sqlalchemy的alembic不';t检测柱的添加和删除
我定义了流动模型Python sqlalchemy的alembic不';t检测柱的添加和删除,python,sqlalchemy,migration,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',
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.5alembic:最新的verison
sqlalchemy:1.0
框架:猎鹰
系统:Debian GNU/Linux 7
首先,我将描述我的环境:
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
之后,我运行了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 ###
"""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, first
之后,您可以从app/models.py
中的表定义中删除jjfq\u service\u providor\u 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
第二个迁移文件应如下所示:
"""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 ###
"""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”是经过深思熟虑的,它帮助我更准确地记录了删除名称
列的过程。我认为,当您试图确定迁移历史时,它会让您更清楚
希望有帮助 首先,我将描述我的环境:
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
之后,我运行了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 ###
"""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, first
之后,您可以从app/models.py
中的表定义中删除jjfq\u service\u providor\u 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
第二个迁移文件应如下所示:
"""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 ###
"""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”是经过深思熟虑的,它帮助我更准确地记录了删除名称
列的过程。我认为,当您试图确定迁移历史时,它会让您更清楚
希望有帮助 噢,我终于找到问题了。My sys.path指向错误的目录,其中是旧版本的代码。所以每次我运行alembic revision--autogenerate时,它都会读取未修改的旧代码。哦,最后我发现了问题。My sys.path指向错误的目录,其中是旧版本的代码。因此,每次我运行
alembic revision--autogenerate
,它都会读取未修改的旧代码。如何在base.py中定义jjfq_service_providor_记录并在model.py中删除jjfq_service_providor_record.name?请告诉我我的答案是否令人满意。如果没有,请说明原因。谢谢,我跟着你走。但第二次迁移仍然是空的。我已经更新了我的问题@约翰·A.@dae-Hmm……这很有趣。请尝试查看alembic current的内容。它应该指向标题
,在本例中是第二次修订版acbba548b6cd
。如果所有其他操作都失败,您应该尝试删除alembic\u version
数据库表并从头开始。哦,我知道为什么。My sys.path指向错误的目录,其中是旧版本的代码。谢谢你的回答@John A.您如何在base.py中定义jjfq_service_providor_记录并在model.py中删除jjfq_service_providor_记录.name?@dae如果我的回答令人满意,请告诉我。如果没有,请说明原因。谢谢,我跟着你走。但第二次迁移仍然是空的。我已经更新了我的问题@约翰·A.@dae-Hmm……这很有趣。请尝试查看alembic current的内容。它应该指向标题
,在本例中是第二次修订版acbba548b6cd
。如果所有其他操作都失败,您应该尝试删除alembic\u version
数据库表并从头开始。哦,我知道为什么。My sys.path指向错误的目录,其中是旧版本的代码。谢谢你的回答@约翰A。