From 74f52f86ce45f187055596832a9bdf1aeb47b99c Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 30 Mar 2021 15:22:01 -0400 Subject: [PATCH] ignore and warn for native_enum=False with pg.ENUM datatype any UPPERCASE datatype refers to that exact type name rendered on the database. So PG's ENUM must render "ENUM" and is "native" by definition. warn if this flag is passed. The :class:`_postgresql.ENUM` datatype is PostgreSQL-native and therefore should not be used with the ``native_enum=False`` flag. This flag is now ignored if passed to the :class:`_postgresql.ENUM` datatype and a warning is emitted; previously the flag would cause the type object to fail to function correctly. Fixes: #6106 Change-Id: I08e0ec6fcfafd068e1eaf6aec13c8010f09ce94a --- doc/build/changelog/unreleased_14/6106.rst | 10 ++++++++++ lib/sqlalchemy/dialects/postgresql/base.py | 8 ++++++++ test/dialect/postgresql/test_types.py | 23 ++++++++++++++++++---- 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 doc/build/changelog/unreleased_14/6106.rst diff --git a/doc/build/changelog/unreleased_14/6106.rst b/doc/build/changelog/unreleased_14/6106.rst new file mode 100644 index 0000000000..9fa60317e7 --- /dev/null +++ b/doc/build/changelog/unreleased_14/6106.rst @@ -0,0 +1,10 @@ +.. change:: + :tags: bug, postgresql + :tickets: 6106 + + The :class:`_postgresql.ENUM` datatype is PostgreSQL-native and therefore + should not be used with the ``native_enum=False`` flag. This flag is now + ignored if passed to the :class:`_postgresql.ENUM` datatype and a warning + is emitted; previously the flag would cause the type object to fail to + function correctly. + diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 82b848ece2..f33542ee80 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1868,6 +1868,14 @@ class ENUM(sqltypes.NativeForEmulated, sqltypes.Enum): be used to emit SQL to a target bind. """ + native_enum = kw.pop("native_enum", None) + if native_enum is False: + util.warn( + "the native_enum flag does not apply to the " + "sqlalchemy.dialects.postgresql.ENUM datatype; this type " + "always refers to ENUM. Use sqlalchemy.types.Enum for " + "non-native enum." + ) self.create_type = kw.pop("create_type", True) super(ENUM, self).__init__(*enums, **kw) diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index 4f87603f3e..1b527df2ce 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -157,6 +157,22 @@ class EnumTest(fixtures.TestBase, AssertsExecutionResults): __only_on__ = "postgresql > 8.3" + def test_native_enum_warnings(self): + """test #6106""" + + with testing.expect_warnings( + "the native_enum flag does not apply to the " + "sqlalchemy.dialects.postgresql.ENUM datatype;" + ): + e1 = postgresql.ENUM("a", "b", "c", native_enum=False) + + e2 = postgresql.ENUM("a", "b", "c", native_enum=True) + e3 = postgresql.ENUM("a", "b", "c") + + is_(e1.native_enum, True) + is_(e2.native_enum, True) + is_(e3.native_enum, True) + def test_create_table(self, metadata, connection): metadata = self.metadata t1 = Table( @@ -2161,10 +2177,9 @@ class ArrayEnum(fixtures.TestBase): @testing.combinations( sqltypes.ARRAY, postgresql.ARRAY, argnames="array_cls" ) - @testing.combinations(sqltypes.Enum, postgresql.ENUM, argnames="enum_cls") - def test_raises_non_native_enums( - self, metadata, connection, array_cls, enum_cls - ): + def test_raises_non_native_enums(self, metadata, connection, array_cls): + enum_cls = sqltypes.Enum + Table( "my_table", self.metadata, -- 2.47.2