python - sqlalchemy postgresql "Is Null"索引

标签 python postgresql sqlalchemy

如何在 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 NULLpartial 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/

相关文章:

python - Django 夹层与 Wordpress

python - 在 QTreeView 中使用自定义角色而不是 DisplayRole

java - PSQL异常 : ERREUR: realtion "table" doesn't exist

sql - PostgreSQL 从具有指定条件的表中选择行

python - 在 SLURM 中运行程序时如何保存打印语句?

python - 如何删除列表中的第一项和最后一项?

node.js - Sequelize 模型不会在 create() 中保留所有属性

带 SQLAlchemy 的 Flask WTForms : UnmappedInstanceError when storing a form in a database

python - sqlalchemy:为什么我不能更新到 func.now(),但可以使用 'now()'?

flask - 使用分页时如何更新值?