【问题标题】:Generic partial Index support for sqlalchemysqlalchemy 的通用部分索引支持
【发布时间】:2017-06-08 21:02:42
【问题描述】:

按照这个问题,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)
    ),

但我看到sqlitehttps://sqlite.org/partialindex.html也支持部分索引

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

【问题讨论】:

    标签: python postgresql sqlite sqlalchemy partial-index


    【解决方案1】:

    没有通用的办法,根据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 初始化索引

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2014-02-08
      • 2015-03-14
      相关资源
      最近更新 更多