Controlling Table Reflection

控制表反射

The Table object that is reflected when “move and copy” proceeds is performed using the standard autoload=True approach. This call can be affected using the reflect_args and reflect_kwargs arguments. For example, to override a Column within the reflection process such that a Boolean object is reflected with the create_constraint flag set to False:

使用标准 autoload=True 方法执行 “移动和复制” 时反射的 Table 对象。 使用 reflect_argsreflect_kwargs 参数可以影响此调用。 例如,要在反射过程中覆盖 Column,以便在将 create_constraint 标志设置为 False 的情况下反射布尔对象:

with self.op.batch_alter_table(
    "bar",
    reflect_args=[Column('flag', Boolean(create_constraint=False))]
) as batch_op:
    batch_op.alter_column(
        'flag', new_column_name='bflag', existing_type=Boolean)

Another use case, add a listener to the Table as it is reflected so that special logic can be applied to columns or types, using the column_reflect() event:

另一个用例,使用 column_reflect() 事件在 反射时向 添加一个侦听器,以便可以将特殊逻辑应用于列或类型:

def listen_for_reflect(inspector, table, column_info):
    "correct an ENUM type"
    if column_info['name'] == 'my_enum':
        column_info['type'] = Enum('a', 'b', 'c')

with self.op.batch_alter_table(
    "bar",
    reflect_kwargs=dict(
        listeners=[
            ('column_reflect', listen_for_reflect)
        ]
    )
) as batch_op:
    batch_op.alter_column(
        'flag', new_column_name='bflag', existing_type=Boolean)

The reflection process may also be bypassed entirely by sending a pre-fabricated Table object; see Working in Offline Mode for an example.

反射过程也可以通过发送一个预制的 Table 对象来完全绕过; 有关示例,请参阅在脱机模式下工作