postgresql - Flask-migrate 和更改列类型

标签 postgresql python-3.x flask alembic

我正在尝试学习一些 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'

我想我可以只复制一个,编一个名字..但是下一个被 flask migrate 拾取的会识别它吗?所以是的.. 没有太多不稳定的黑客攻击的正确处理方法是什么?

最佳答案

只需要在Migrate构造函数中加入compare_type=True

from flask_migrate import Migrate
migrate = Migrate(compare_type=True)

app = Flask(__name__)
migrate.init_app(app)

关于postgresql - Flask-migrate 和更改列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33944436/

相关文章:

sql - 如何提取去极端化的常见行为?

SQL - 按组删除除最后 N 行之外的所有行

Spring Boot、PostgreSQL 和 Docker - 在容器中运行时连接被拒绝

postgresql - 在扣除用户余额和调用第三方服务时如何正确解释系统故障?

python - 键入多种类型的提示值?

python - 我有多个 flask 微服务彼此通信,如何配置docker?

python - Jinja2圆形过滤器不四舍五入

python - 将列表中的项目从前移到后

python - Odoo 13 中@api.one 的替代方案是什么?

python - 在 Python Flask 中维护 session 数据