From: Mike Bayer Date: Tue, 25 Sep 2018 14:38:40 +0000 (-0400) Subject: Copy create_constraint flag for Enum X-Git-Tag: rel_1_3_0b1~66^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0737f45d4ff8fdb2e12972cc58c18345e4d6dde2;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Copy create_constraint flag for Enum Fixed bug where the :paramref:`.Enum.create_constraint` flag on the :class:`.Enum` datatype would not be propagated to copies of the type, which affects use cases such as declarative mixins and abstract bases. Fixes: #4341 Change-Id: I978be65f33a616fe4d5f5de03fb3eaab6f6a2272 --- diff --git a/doc/build/changelog/unreleased_12/4341.rst b/doc/build/changelog/unreleased_12/4341.rst new file mode 100644 index 0000000000..c3db81b206 --- /dev/null +++ b/doc/build/changelog/unreleased_12/4341.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, sql + :tickets: 4341 + + Fixed bug where the :paramref:`.Enum.create_constraint` flag on the + :class:`.Enum` datatype would not be propagated to copies of the type, which + affects use cases such as declarative mixins and abstract bases. diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 08af78606a..e35ed6d1e1 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -1057,7 +1057,6 @@ class SchemaType(SchemaEventTarget): schema = kw.pop('schema', self.schema) metadata = kw.pop('metadata', self.metadata) _create_events = kw.pop('_create_events', False) - return impltype(name=self.name, schema=schema, inherit_schema=self.inherit_schema, @@ -1442,6 +1441,7 @@ class Enum(Emulated, String, SchemaType): kw.setdefault('_create_events', False) kw.setdefault('native_enum', self.native_enum) kw.setdefault('values_callable', self.values_callable) + kw.setdefault('create_constraint', self.create_constraint) assert '_enums' in kw return impltype(**kw) diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index da133b21ab..4976e2fb10 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -15,7 +15,7 @@ import sqlalchemy as tsa from sqlalchemy.testing import fixtures from sqlalchemy import testing from sqlalchemy.testing import ComparesTables, AssertsCompiledSQL -from sqlalchemy.testing import eq_, is_, mock, is_true +from sqlalchemy.testing import eq_, is_, mock, is_true, is_false from contextlib import contextmanager from sqlalchemy import util from sqlalchemy.testing import engines @@ -1879,6 +1879,18 @@ class SchemaTypeTest(fixtures.TestBase): m.dispatch.before_create(t1, testing.db) eq_(t1.c.y.type.evt_targets, (t1, )) + def test_enum_nonnative_column_copy_transfers_constraintpref(self): + m = MetaData() + + type_ = self.WrapEnum( + 'a', 'b', 'c', name='foo', + native_enum=False, create_constraint=False) + y = Column('y', type_) + y_copy = y.copy() + Table('x', m, y_copy) + + is_false(y_copy.type.create_constraint) + def test_boolean_column_copy_transfers_events(self): m = MetaData() @@ -1889,6 +1901,16 @@ class SchemaTypeTest(fixtures.TestBase): is_true(y_copy.type._create_events) + def test_boolean_nonnative_column_copy_transfers_constraintpref(self): + m = MetaData() + + type_ = self.WrapBoolean(create_constraint=False) + y = Column('y', type_) + y_copy = y.copy() + Table('x', m, y_copy) + + is_false(y_copy.type.create_constraint) + def test_metadata_dispatch_no_new_impl(self): m1 = MetaData() typ = self.MyType(metadata=m1)