Skip to content

postgresql ExcludeConstraint behave differently with --autogenerate compare to sqlalchemy Base.metadata.create_all #1230

Description

@tc-yu

Describe the bug
when running alembic revision --autogenerate, on a table with

ExcludeConstraint(
    (func.tstzrange(effective_time, expiry_time), "&&"),
    using="gist",
)

it generates

    postgresql.ExcludeConstraint(('tstzrange(effective_time, expiry_time)', '&&'), using='gist'),

in the revision script, resulting in sqlalchemy.exc.ConstraintColumnNotFoundError: Can't create ExcludeConstraint on table 'time_slot': no column named 'tstzrange(effective_time, expiry_time)' is present.

Adding sqlalchemy.text around the string does fix the issue

Expected behavior
the table definition works with sqlalchemy's Base.metadata.create_all(bind=engine), creating

"time_slot_tstzrange_excl" EXCLUDE USING gist (tstzrange(effective_time, expiry_time) WITH &&)

in the database so I expect it works the same in alembic

To Reproduce

I setup a minimal example in this repo: https://github.com/tc-yu/alembic-pgsql-tztsrange

model:

from datetime import datetime

from sqlalchemy import DateTime, Identity, func
from sqlalchemy.dialects.postgresql import ExcludeConstraint
from sqlalchemy.orm import Mapped, declarative_base, mapped_column

class TimeSlot(Base):
    __tablename__ = "time_slot"

    id: Mapped[int] = mapped_column(Identity(), primary_key=True)

    effective_time: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), index=True
    )
    expiry_time: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), index=True
    )

    __table_args__ = (
        ExcludeConstraint(
            (func.tstzrange(effective_time, expiry_time), "&&"),
            using="gist",
        ),
    )

Error

sqlalchemy.exc.ConstraintColumnNotFoundError: Can't create ExcludeConstraint on table 'time_slot': no column named 'tstzrange(effective_time, expiry_time)' is present.

Versions.

  • OS: macOS
  • Python: 3.11.3
  • Alembic: 1.10.4
  • SQLAlchemy: 2.0.11
  • Database: postgresql 15.2
  • DBAPI: psycopg 3.1.8

Additional context
This is the first time I work with ExcludeConstraint, so it might just be me using the wrong way to define it in the model

Have a nice day!

Activity

  1. CaselIT commented on Apr 28, 2023

    @CaselIT
    Member

    Hi,

    Thanks for reporting.

  2. sqla-tester commented on Apr 28, 2023

    @sqla-tester
    Collaborator

    Federico Caselli has proposed a fix for this issue in the main branch:

    Fix ExcludeConstraint with func. https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4591

  3. added a commit that references this issue on Sep 27, 2024
    ac0785d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions