sqlalchemy 的通用部分索引支持

Generic partial Index support for sqlalchemy

根据这个问题,SQLAlchemy - SQLite for testing and Postgresql for development - How to port?

我意识到,(以上)共识是不要使用未在生产中使用的数据库进行测试

我想提取 部分索引sqlalchemy 的支持,这样我就可以使用 Postgres 或 Sqlite。

我已经看到 PostgreSQL 我可以使用

    Index('only_one_active_invoice', 
          invoice_id, active,
          unique=True,
          postgresql_where=(active)
    ),

但我看到 sqlite https://sqlite.org/partialindex.html

也支持部分索引

是否有某种 genericsqlalchemy 的部分索引支持,我的模块可以用于 postgres 或 sqlite 数据库?

没有通用的方法,根据doc

你应该使用 sqlite

idx = Index('test_idx1', tbl.c.data,
            sqlite_where=and_(tbl.c.data > 5, tbl.c.data < 10))

对于 postgres:

idx = Index('my_index', my_table.c.id, 
            postgresql_where=my_table.c.value > 10)

对于您的情况,您可以检查数据库引擎并使用 kwargs 初始化索引