postgresql – Flask-migrate和更改列类型
发布时间:2020-12-13 16:07:09 所属栏目:百科 来源:网络整理
导读:我正在尝试学习一些Flask,我正在使用Flask-Migrate 1.6.0 所以我制作了一个看起来像这样的模型 class Download(db.Model):__tablename__ = "downloads"id = db.Column(db.Integer,autoincrement=True,primary_key=True)filename = db.Column(db.String,nulla
我正在尝试学习一些Flask,我正在使用Flask-Migrate 1.6.0
所以我制作了一个看起来像这样的模型 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) 然后我将它更改为完全相同的东西,除了这一行: size = db.Column(db.BigInteger,nullable=False) 当我跑我的 manager.py db migrate 命令它不会检测列类型的更改.我已经阅读了它,我知道它应该在我更改env.py并添加compare_type = True变量时将其取出.但我这样做无济于事,这个方法现在看起来像这样 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文件时做错了什么? 如果我没有,它仍然没有接受它,我如何完全手动进行下一次迁移修订?因为我的migrate文件夹中的修订版具有如下所示的名称,并且其中的内容就像这样 # revision identifiers,used by Alembic. revision = '7e9f264b0f' down_revision = '2e59d536f50' 我想我可以复制一个,组成一个名字..但是下一个由烧瓶迁移的人会认出它吗?所以是的..没有太多的黑客攻击,处理它的正确方法是什么? 解决方法
默认情况下,自动生成修订时,Alembic无法识别列类型更改等内容.进行这些更细化的更改时,您需要手动修改迁移以包含这些更改
例如,在您的迁移文件中 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',existing_type=sa.BigInteger(),type_=sa.Integer()) 有关操作的详细信息,请参见the operation reference 您可以通过修改env.py和alembic.ini来启用类型更改检测,如here所示 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |