Skip to content

Can't use a CAST in a PostgreSQL ExcludeConstraint #3454

@sqlalchemy-bot

Description

@sqlalchemy-bot

Migrated issue, originally created by v (@vr2262)

In PostgreSQL I can create a table with an exclusion constraint involving a CAST (the CAST is necessary because the UUID type doesn't have a default operator class for gist):

CREATE EXTENSION btree_gist;
CREATE TABLE example (
  id         UUID,
  some_range INT4RANGE,
  EXCLUDE USING gist (CAST("id" AS TEXT) WITH =, some_range WITH &&)
);

When I try to do the same in SQLAlchemy:

from sqlalchemy import *
from sqlalchemy.dialects.postgresql import (
    UUID, INT4RANGE, ExcludeConstraint, TEXT
)

class Example(Base):
    __tablename__ = 'example'
    id = Column(UUID)
    some_range = Column(INT4RANGE)
    __table_args__ = (
        ExcludeConstraint(
            (cast('id', TEXT), '='), ('some_range', '&&')
        ),
    )

I get the error sqlalchemy.exc.ArgumentError: Can't add unnamed column to column collection.

('id::TEXT', '=') doesn't work because SQLAlchemy doesn't recognize 'id::TEXT' as a column.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions