]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
PostgreSQL enum with no elements returns NULL for the "label", skip this
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 31 May 2019 00:42:35 +0000 (20:42 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 31 May 2019 00:42:35 +0000 (20:42 -0400)
Fixed bug where PostgreSQL dialect could not correctly reflect an ENUM
datatype that has no members, returning a list with ``None`` for the
``get_enums()`` call and raising a TypeError when reflecting a column which
has such a datatype.   The inspection now returns an empty list.

Fixes: #4701
Change-Id: I202bab19728862cbc64deae211d5ba6a103b8317

doc/build/changelog/unreleased_13/4701.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_13/4701.rst b/doc/build/changelog/unreleased_13/4701.rst
new file mode 100644 (file)
index 0000000..ff14e7b
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, postgresql
+    :tickets: 4701
+
+    Fixed bug where PostgreSQL dialect could not correctly reflect an ENUM
+    datatype that has no members, returning a list with ``None`` for the
+    ``get_enums()`` call and raising a TypeError when reflecting a column which
+    has such a datatype.   The inspection now returns an empty list.
index f18bec932b4f772a343ae66f47eee94ee4a182bb..1363e81af7cb2597f5a344a9e027ea05686ecfc1 100644 (file)
@@ -3470,8 +3470,10 @@ class PGDialect(default.DefaultDialect):
                     "name": enum["name"],
                     "schema": enum["schema"],
                     "visible": enum["visible"],
-                    "labels": [enum["label"]],
+                    "labels": [],
                 }
+                if enum["label"] is not None:
+                    enum_rec["labels"].append(enum["label"])
                 enums.append(enum_rec)
         return enums
 
index ae1dff6d058443522dee8b684d36a1b0cadf4fd8..f2e4911679777608b672b2474e20c74c3a087926 100644 (file)
@@ -1290,6 +1290,33 @@ class ReflectionTest(fixtures.TestBase):
             ],
         )
 
+    @testing.provide_metadata
+    def test_inspect_enum_empty(self):
+        enum_type = postgresql.ENUM(name="empty", metadata=self.metadata)
+        enum_type.create(testing.db)
+        inspector = reflection.Inspector.from_engine(testing.db)
+
+        eq_(
+            inspector.get_enums(),
+            [
+                {
+                    "visible": True,
+                    "labels": [],
+                    "name": "empty",
+                    "schema": "public",
+                }
+            ],
+        )
+
+    @testing.provide_metadata
+    def test_inspect_enum_empty_from_table(self):
+        Table(
+            "t", self.metadata, Column("x", postgresql.ENUM(name="empty"))
+        ).create(testing.db)
+
+        t = Table("t", MetaData(testing.db), autoload_with=testing.db)
+        eq_(t.c.x.type.enums, [])
+
     @testing.provide_metadata
     @testing.only_on("postgresql >= 8.5")
     def test_reflection_with_unique_constraint(self):