Why Flask-migrate cannot upgrade when drop column

前端 未结 3 1691
别那么骄傲
别那么骄傲 2020-12-23 10:03

I am using SqlAlchemy and Flask-migrate for DB migration. I have successfully init the DB and upgrade once, but when I deleted one of my table colu

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 10:24

    change the migrations/env.py add render_as_batch=True in context.configure

    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,
                          process_revision_directives=process_revision_directives,
                          render_as_batch=True,# this is new feature
                          **current_app.extensions['migrate'].configure_args)
    
        try:
            with context.begin_transaction():
                context.run_migrations()
        finally:
            connection.close()
    

提交回复
热议问题