diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-05-12 14:34:00 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-05-12 15:09:33 -0400 |
| commit | 11a1d5e70450300310ff7fafeeb03fe1baa9380e (patch) | |
| tree | 1dcb0c0e3ee15763d7434b28229201218859be8a | |
| parent | 169d6e6804c0e6bbb5f8b2fa4217e659b49a0d9f (diff) | |
| download | sqlalchemy-11a1d5e70450300310ff7fafeeb03fe1baa9380e.tar.gz | |
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
| -rw-r--r-- | doc/build/changelog/unreleased_20/9773.rst | 9 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/named_types.py | 3 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/type_api.py | 2 | ||||
| -rw-r--r-- | test/dialect/postgresql/test_types.py | 19 |
4 files changed, 33 insertions, 0 deletions
diff --git a/doc/build/changelog/unreleased_20/9773.rst b/doc/build/changelog/unreleased_20/9773.rst new file mode 100644 index 000000000..839bfaab6 --- /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 92aba6f63..4412d841b 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 5af12cb93..312034e9d 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 f322bf354..5df8bc0a5 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( |
