【问题标题】:Can Alembic Autogenerate column alterations?Alembic 可以自动生成列更改吗?
【发布时间】:2013-06-15 00:10:51
【问题描述】:

我可以使用 alembic --autogenerate 用于添加/删除列时。

但是,例如,当我想将“url”列从 200 个字符修改为 2000 个字符时,它不会检测到更改。

如何制作 Alembic(使用 SQLAlchemy)、检测更改并自动生成脚本到我的模型的各种列的“大小”并为 PostgreSQL 创建“alter_column”命令??

编辑:

为什么alembic没有自动添加:

op.alter_column('mytable', 'url', type_=sa.String(2000), existing_type=sa.String(length=200), nullable=True)

【问题讨论】:

    标签: python postgresql sqlalchemy flask alembic


    【解决方案1】:

    我也遇到过这个问题,在alembic 1.0.8def run_migrations_online() 文件中的context.configure migrations/env.py 函数会是这样的:

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            process_revision_directives=process_revision_directives,
            **current_app.extensions['migrate'].configure_args,
        )
    

    只需删除或评论process_revision_directives=process_revision_directives,然后在上面添加compare_type=True

    像这样:

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            # process_revision_directives=process_revision_directives,
            **current_app.extensions['migrate'].configure_args,
            compare_type=True
        )
    

    【讨论】:

    • 您能否详细说明为什么此解决方案需要注释掉 theprocess_revision_directives=process_revision_directives 行?谢谢
    【解决方案2】:

    看起来我在 reddit 的 /r/flask 上找到了答案。

    http://www.reddit.com/r/flask/comments/1glejl/alembic_autogenerate_column_changes/cale9o0

    只需将“compare_type=True”添加到 env.py 的“run_migrations_online”函数中的 context.configure() 参数即可。

        context.configure(
                    connection=connection,
                    target_metadata=target_metadata,
                    compare_type=True
                    )
    

    【讨论】:

    • 这确实需要成为默认设置,或者至少在如何打开它方面更加明显。
    • 这是我的救命稻草。
    猜你喜欢
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多