擅长:python、mysql、java
<p>如果使用的是Alembic,则可以创建一个迁移来执行此操作,并完全避免将其添加到模型中:</p>
<pre><code>def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_index('table1_id', 'table1', ['id'], unique=True)
op.create_index('table2_id', 'table2', ['id'], unique=True)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index('table1_id', table_name='table1')
op.drop_index('table2_id', table_name='table2')
# ### end Alembic commands ###
</code></pre>