34

添加/删除列时我可以使用 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)
4

2 回答 2

67

看起来我在 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
                )
于 2013-06-18T19:03:41.427 回答
1

我也遇到过这个问题,文件上alembic 1.0.8context.configureindef run_migrations_online()函数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
    )
于 2019-08-06T14:14:52.637 回答