diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-05-30 20:42:35 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-05-30 20:42:35 -0400 |
| commit | 754e7f52cf64b72988bdf8211c603809b32c16de (patch) | |
| tree | e925e74538d9d387f20f8abb3a0771b6f5be59b6 | |
| parent | df99e1ef5f334ce7f4c8118c3e0bdf2949f54de3 (diff) | |
| download | sqlalchemy-754e7f52cf64b72988bdf8211c603809b32c16de.tar.gz | |
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
| -rw-r--r-- | doc/build/changelog/unreleased_13/4701.rst | 8 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 4 | ||||
| -rw-r--r-- | test/dialect/postgresql/test_reflection.py | 27 |
3 files changed, 38 insertions, 1 deletions
diff --git a/doc/build/changelog/unreleased_13/4701.rst b/doc/build/changelog/unreleased_13/4701.rst new file mode 100644 index 000000000..ff14e7b08 --- /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 f18bec932..1363e81af 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -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 diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index ae1dff6d0..f2e491167 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -1291,6 +1291,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): insp = inspect(testing.db) |
