From: Federico Caselli Date: Mon, 21 Jul 2025 21:36:43 +0000 (+0200) Subject: Fix reflection of enum named intervalsomething X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0620f7d35251befc72247370820e1ac9a931440e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix reflection of enum named intervalsomething Fixes bug that would mistakenly interpret a domain or enum type with name starting in ``interval`` as an ``INTERVAL`` type while reflecting a table. Fixes: #12744 Change-Id: I89ab287c3847ca545691afe73f26d86bf2337ae0 --- diff --git a/doc/build/changelog/unreleased_20/12744.rst b/doc/build/changelog/unreleased_20/12744.rst new file mode 100644 index 0000000000..0adbdc71f2 --- /dev/null +++ b/doc/build/changelog/unreleased_20/12744.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, reflection, postgresql + :tickets: 12744 + + Fixes bug that would mistakenly interpret a domain or enum type + with name starting in ``interval`` as an ``INTERVAL`` type while + reflecting a table. diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 43251ce277..d06b131a62 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -3941,7 +3941,8 @@ class PGDialect(default.DefaultDialect): charlen = int(attype_args[0]) args = (charlen,) - elif attype.startswith("interval"): + # a domain or enum can start with interval, so be mindful of that. + elif attype == "interval" or attype.startswith("interval "): schema_type = INTERVAL field_match = re.match(r"interval (.+)", attype) diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index 5dd8e00070..7061b6e0fc 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -2237,6 +2237,17 @@ class ReflectionTest( t = Table("t", MetaData(), autoload_with=connection) eq_(t.c.x.type.enums, []) + def test_enum_starts_with_interval(self, metadata, connection): + """Test for #12744""" + enum_type = postgresql.ENUM("day", "week", name="intervalunit") + t1 = Table("t1", metadata, Column("col", enum_type)) + t1.create(connection) + + insp = inspect(connection) + cols = insp.get_columns("t1") + is_true(isinstance(cols[0]["type"], postgresql.ENUM)) + eq_(cols[0]["type"].enums, ["day", "week"]) + def test_reflection_with_unique_constraint(self, metadata, connection): insp = inspect(connection)