Batch mode with Autogenerate

自动生成和批处理模式

The syntax of batch mode is essentially that Operations.batch_alter_table() is used to enter a batch block, and the returned BatchOperations context works just like the regular Operations context, except that the “table name” and “schema name” arguments are omitted.

批处理模式的语法本质上是 Operations.batch_alter_table() 用于进入批处理块,返回的 BatchOperations 上下文与常规操作上下文一样工作,只是省略了 “table name” 和 “schema name” 参数。

To support rendering of migration commands in batch mode for autogenerate, configure the EnvironmentContext.configure.render_as_batch flag in env.py:

要支持以批处理模式渲染迁移命令以进行自动生成,请在 env.py 中配置 EnvironmentContext.configure.render_as_batch 标志:

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

Autogenerate will now generate along the lines of:

Autogenerate 现在将按照以下方式生成:

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('address', schema=None) as batch_op:
        batch_op.add_column(sa.Column('street', sa.String(length=50), nullable=True))

This mode is safe to use in all cases, as the Operations.batch_alter_table() directive by default only takes place for SQLite; other backends will behave just as they normally do in the absence of the batch directives.

此模式在所有情况下都可以安全使用,因为默认情况下 Operations.batch_alter_table() 指令仅适用于 SQLite; 在没有批处理指令的情况下,其他后端的行为将与它们通常的行为一样。

Note that autogenerate support does not include “offline” mode, where the Operations.batch_alter_table.copy_from parameter is used. The table definition here would need to be entered into migration files manually if this is needed.

请注意,自动生成支持不包括使用 Operations.batch_alter_table.copy_from 参数的“离线”模式。 如果需要,此处的表定义需要手动输入到迁移文件中。