]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix reflection of enum named intervalsomething
authorFederico Caselli <cfederico87@gmail.com>
Mon, 21 Jul 2025 21:36:43 +0000 (23:36 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Mon, 28 Jul 2025 16:41:03 +0000 (18:41 +0200)
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
(cherry picked from commit 0620f7d35251befc72247370820e1ac9a931440e)

doc/build/changelog/unreleased_20/12744.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_reflection.py

diff --git a/doc/build/changelog/unreleased_20/12744.rst b/doc/build/changelog/unreleased_20/12744.rst
new file mode 100644 (file)
index 0000000..0adbdc7
--- /dev/null
@@ -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.
index 156dccb32fe4b56186591ae4558527d61b4beaa9..25570c21bb3570ae6d1c16ab6c9331d1df293768 100644 (file)
@@ -3935,7 +3935,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)
index 5dd8e00070d6a2397ae6766176816ccda33323ee..7061b6e0fc6608de6735f2bb3726c0cee295ff2d 100644 (file)
@@ -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)