From: Mike Bayer Date: Fri, 31 May 2019 00:42:35 +0000 (-0400) Subject: PostgreSQL enum with no elements returns NULL for the "label", skip this X-Git-Tag: rel_1_3_5~12^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97f2abb545f42b90c2f045bcc28b7e74dbdb5567;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git PostgreSQL enum with no elements returns NULL for the "label", skip this 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 (cherry picked from commit 754e7f52cf64b72988bdf8211c603809b32c16de) --- diff --git a/doc/build/changelog/unreleased_13/4701.rst b/doc/build/changelog/unreleased_13/4701.rst new file mode 100644 index 0000000000..ff14e7b085 --- /dev/null +++ b/doc/build/changelog/unreleased_13/4701.rst @@ -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. diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index ceb6246442..fe9085ae57 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -3467,8 +3467,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 diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index ae1dff6d05..f2e4911679 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -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):