From: Mike Bayer Date: Fri, 12 May 2023 18:34:00 +0000 (-0400) Subject: include create_type in pg.ENUM.adapt; test all attrs X-Git-Tag: rel_2_0_14~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=11a1d5e70450300310ff7fafeeb03fe1baa9380e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git include create_type in pg.ENUM.adapt; test all attrs Fixed apparently very old issue where the :paramref:`_postgresql.ENUM.create_type` parameter, when set to its non-default of ``False``, would not be propagated when the :class:`_schema.Column` which it's a part of were copied, as is common when using ORM Declarative mixins. Fixes: #9773 Change-Id: I79a7c6f052ec39b42400d92bf591c791feca573b --- diff --git a/doc/build/changelog/unreleased_20/9773.rst b/doc/build/changelog/unreleased_20/9773.rst new file mode 100644 index 0000000000..839bfaab66 --- /dev/null +++ b/doc/build/changelog/unreleased_20/9773.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, postgresql + :tickets: 9773 + + Fixed apparently very old issue where the + :paramref:`_postgresql.ENUM.create_type` parameter, when set to its + non-default of ``False``, would not be propagated when the + :class:`_schema.Column` which it's a part of were copied, as is common when + using ORM Declarative mixins. diff --git a/lib/sqlalchemy/dialects/postgresql/named_types.py b/lib/sqlalchemy/dialects/postgresql/named_types.py index 92aba6f63b..4412d841b7 100644 --- a/lib/sqlalchemy/dialects/postgresql/named_types.py +++ b/lib/sqlalchemy/dialects/postgresql/named_types.py @@ -312,6 +312,9 @@ class ENUM(NamedType, sqltypes.NativeForEmulated, sqltypes.Enum): kw.setdefault("values_callable", impl.values_callable) kw.setdefault("omit_aliases", impl._omit_aliases) kw.setdefault("_adapted_from", impl) + if type_api._is_native_for_emulated(impl.__class__): + kw.setdefault("create_type", impl.create_type) + return cls(**kw) def create(self, bind=None, checkfirst=True): diff --git a/lib/sqlalchemy/sql/type_api.py b/lib/sqlalchemy/sql/type_api.py index 5af12cb934..312034e9d2 100644 --- a/lib/sqlalchemy/sql/type_api.py +++ b/lib/sqlalchemy/sql/type_api.py @@ -1482,6 +1482,8 @@ class Emulated(TypeEngineMixin): # as only the default logic is implemented. return cls.adapt_native_to_emulated(self, **kw) else: + # this would be, both classes are Enum, or both classes + # are postgresql.ENUM if issubclass(cls, self.__class__): return self.adapt_to_emulated(cls, **kw) else: diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index f322bf3548..5df8bc0a5b 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -231,6 +231,25 @@ class NamedTypeTest( is_(e2.native_enum, True) is_(e3.native_enum, True) + @testing.combinations( + ("name", "foobar", "name"), + ("validate_strings", True, "validate_strings"), + ("omit_aliases", False, "_omit_aliases"), + ("create_type", False, "create_type"), + ("create_type", True, "create_type"), + ("schema", "someschema", "schema"), + ("inherit_schema", True, "inherit_schema"), + ("metadata", MetaData(), "metadata"), + ("values_callable", lambda x: None, "values_callable"), + ) + def test_enum_copy_args(self, argname, value, attrname): + kw = {argname: value} + e1 = ENUM("a", "b", "c", **kw) + + e1_copy = e1.copy() + + eq_(getattr(e1_copy, attrname), value) + def test_enum_create_table(self, metadata, connection): metadata = self.metadata t1 = Table(