Skip to content

PostgreSQL check constraint reflection strips brackets naively #13157

Description

@RazerM

Describe the bug

Check constraint reflection strips brackets from some expressions such that they are syntactically invalid

Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected

No response

SQLAlchemy Version in Use

2.0.48

DBAPI (i.e. the database driver)

psycopg2

Database Vendor and Major Version

PostgreSQL 17

Python Version

3.14

Operating system

macOS

To Reproduce

from sqlalchemy import (
    CheckConstraint,
    Column,
    Integer,
    MetaData,
    Table,
    create_engine,
    inspect,
)
from sqlalchemy.schema import CreateTable


def main() -> None:
    engine = create_engine("postgresql:///issue13157")

    foo = Table(
        "foo",
        MetaData(),
        Column("id", Integer),
        Column("x", Integer),
        Column("y", Integer),
        CheckConstraint(
            "(x IS NULL OR y IS NULL) AND (x IS NULL OR y IS NULL)",
            name="broken",
        ),
    )

    with engine.connect() as conn:
        conn.execute(CreateTable(foo))
        inspector = inspect(conn)
        (broken,) = inspector.get_check_constraints(foo.name)
        print(broken["sqltext"])


if __name__ == "__main__":
    main()

Error

output:

x IS NULL OR y IS NULL) AND (x IS NULL OR y IS NULL

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggreat mcveAn issue with a great mcvepostgresqlreflectionreflection of tables, columns, constraints, defaults, sequences, views, everything else

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions