sqlalchemy postgresql“为空"指数 [英] sqlalchemy postgresql "Is Null" index

查看:61
本文介绍了sqlalchemy postgresql“为空"指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 PostgreSQL 上使用 sqlalchemy 创建 IS NULL 索引?

How can you create an IS NULL index using sqlalchemy on PostgreSQL?

PostgreSQL 支持这一点:

PostgreSQL supports this:

索引列上的 IS NULL 或 IS NOT NULL 条件可以与 B 树索引一起使用.

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 在这种特定情况下是 部分索引.您可以通过 在 SQLAlchemy 中为 PostgreSQL 声明部分索引postresql_where kwarg:

IS NULL is not a separate index type. IS NULL in this specific case is a condition expression on a partial index. You can declare partial indices for PostgreSQL in SQLAlchemy via the postresql_where kwarg:

Index('processing_null_index', processing_start_time, postgresql_where=processing_start_time.is_(None))

这篇关于sqlalchemy postgresql“为空"指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆