JDiMatteo
JDiMatteo

Reputation: 13130

sqlalchemy postgresql "Is Null" index

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

PostgreSQL supports this:

an IS NULL or IS NOT NULL condition on an index column can be used with a B-tree index.

I've tried the following:

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),
    )

I get this error:

/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))

Upvotes: 2

Views: 1534

Answers (1)

univerio
univerio

Reputation: 20498

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))

Upvotes: 3

Related Questions