Dag*_*ahl 5 postgresql sqlalchemy alembic
我有一个 Alembic 迁移,它创建了一些数据库中缺少的数据库索引。例子:
op.create_index(op.f('ix_some_index'), 'table_1', ['column_1'], unique=False)
Run Code Online (Sandbox Code Playgroud)
但是,在其他已有索引的环境中迁移失败:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "ix_some_index" already exists
Run Code Online (Sandbox Code Playgroud)
PostgreSQL 支持IF NOT EXISTS此类情况的选项,但我没有看到任何使用 Alembic 或 SQLAlchemy 选项调用它的方法。是否有检查现有索引的规范方法?
小智 1
目前,您可以将if_not_exists参数传递给 Alembic 中的 create_index 函数,如下所示:
op.create_index('ix_some_index', 'table_1', ['column_1'], unique=False, if_not_exists=True)
Run Code Online (Sandbox Code Playgroud)
同样,为了删除索引,您可以使用if_exists参数:
op.drop_index('ix_some_index', if_exists=True)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4292 次 |
| 最近记录: |