]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
ignore and warn for native_enum=False with pg.ENUM datatype
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 30 Mar 2021 19:22:01 +0000 (15:22 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 10 Sep 2021 14:41:41 +0000 (10:41 -0400)
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 [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_types.py

diff --git a/doc/build/changelog/unreleased_14/6106.rst b/doc/build/changelog/unreleased_14/6106.rst
new file mode 100644 (file)
index 0000000..9fa6031
--- /dev/null
@@ -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.
+
index 82b848ece2e107b69d70618439b994094c4b8478..f33542ee80f304263131a14de6cd20bb1fcedebc 100644 (file)
@@ -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)
 
index 4f87603f3e9302d79c67adb30124c5483782fd73..1b527df2cef98a00da85648d3839779ed37ea1a1 100644 (file)
@@ -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,