问题描述
我正在尝试学习一些Flask,并且正在使用Flask-Migrate 1.6.0
I am trying to learn some Flask and I am using Flask-Migrate 1.6.0
所以我制作了一个看起来像这样的模型
So I made a model which looks like this
class Download(db.Model):
__tablename__ = "downloads"
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
filename = db.Column(db.String, nullable=False)
size = db.Column(db.Integer, nullable=False)
location = db.Column(db.String, nullable=False)
season = db.Column(db.Integer, nullable=False)
download_timestamp = db.Column(db.DateTime, nullable=False)
show_id = db.Column(db.Integer, ForeignKey("shows.id"))
def __init__(self,filename,size,location,timestamp,season):
self.filename = filename
self.size = size
self.location = location
self.download_timestamp = timestamp
self.season = season
def __repr__(self):
return '<File {}'.format(self.filename)
然后我将其更改为完全相同的东西,除了以下内容:
I have then changed it to the exact same thing except for this line :
size = db.Column(db.BigInteger, nullable=False)
当我运行
manager.py db migrate
命令不会检测到列类型的更改。我已经阅读了它,我知道当我更改env.py并添加compare_type = True变量时,它应该将其选中。但是我没有用,方法现在看起来像这样
command it doesn't detect the change in the column type. And I have read up on it and I know it should pick it up when I change my env.py and add the compare_type=True variable. But I did this to no avail, the method looks like this right now
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
# this callback is used to prevent an auto-migration from being generated
# when there are no changes to the schema
# reference: http://alembic.readthedocs.org/en/latest/cookbook.html
def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, 'autogenerate', False):
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
logger.info('No changes in schema detected.')
engine = engine_from_config(config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
connection = engine.connect()
context.configure(connection=connection,
target_metadata=target_metadata,
compare_type=True,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args)
try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()
好吧,我的问题是:
在更改env.py文件时我做错了吗?
Did I do something wrong in changing the env.py file?
如果没有,但仍然不了解,该如何准确地手动进行下一个迁移修订?因为我的migration文件夹中的修订版的名称如下所示
If I didn't and it still doesn't pick up on it how do I exactly manually make the next migration revision? Because the revisions in my migrate folder have names like below and stuff in it like this
# revision identifiers, used by Alembic.
revision = '7e9f264b0f'
down_revision = '2e59d536f50'
我想我只能复制一个,组成一个名字..但是被烧瓶拾取的下一个会迁移吗?那么,是的..没有太多恶意攻击的正确处理方法是什么?
I guess I could just copy one, make up a name .. but would the next one that is picked up by flask migrate recognize it? So yeah.. what is the correct way of handling it without too much iffy hacking?
推荐答案
更新:
正如预期的那样,此答案已过期,请参阅进行真正的修复!
As expected, this answer is now out of date, please see https://stackoverflow.com/a/52181159/428907 for a real fix!
原始答案:
默认情况下,自动生成修订时,Alembic无法识别列类型更改之类的内容。在进行这些更细粒度的更改时,您将需要手动修改迁移,以将这些更改包括在内
Alembic does not recognize things like column type changes when auto-generating revisions by default. When making these more granular changes, you will need to hand-modify the migration to include these changes
例如,在您的迁移文件中
e.g., in your migration file
from alembic import op
import sqlalchemy as sa
def upgrade():
# ...
op.alter_column('downloads', 'size', existing_type=sa.Integer(), type_=sa.BigInteger())
def downgrade():
# ...
op.alter_column('downloads', 'size', existing_type=sa.BigInteger(), type_=sa.Integer())
有关操作的详细信息,请参见
For details on the operations, see the operation reference
您可以通过修改env.py和alembic.ini来打开类型更改的检测,如
You can turn on detection of type changes by modifying your env.py and alembic.ini, as seen here
这篇关于烧瓶迁移和更改列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!