如何在 PostgreSQL 上使用 sqlalchemy 创建 IS NULL
索引?
PostgreSQL supports this :
an IS NULL or IS NOT NULL condition on an index column can be used with a B-tree index.
我尝试过以下方法:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Foo(db.Model):
id = Column(Integer, primary_key=True)
processing_start_time = Column(DateTime, nullable=True)
__table_args__ = (
Index('processing_null_index', 'processing_start_time', is_null=True),
)
我收到此错误:
/foo/venv/lib/python3.5/site-packages/sqlalchemy/sql/base.py:291: SAWarning: Can't validate argument 'is_null'; can't locate any SQLAlchemy dialect named 'is'
(k, dialect_name))
最佳答案
IS NULL
不是单独的索引类型。在这种特定情况下,IS NULL
是 partial index 上的条件表达式。 。您可以通过 postresql_where
在 SQLAlchemy 中声明 PostgreSQL 的部分索引夸格:
Index('processing_null_index', processing_start_time, postgresql_where=processing_start_time.is_(None))
关于python - sqlalchemy postgresql "Is Null"索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49990520/