Skip to content

Commit

Permalink
Copy create_constraint flag for Enum
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zzzeek committed Sep 25, 2018
1 parent fb991a4 commit 0737f45
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
7 changes: 7 additions & 0 deletions doc/build/changelog/unreleased_12/4341.rst
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion lib/sqlalchemy/sql/sqltypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,6 @@ def adapt(self, impltype, **kw):
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,
Expand Down Expand Up @@ -1442,6 +1441,7 @@ def adapt_to_emulated(self, impltype, **kw):
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)

Expand Down
24 changes: 23 additions & 1 deletion test/sql/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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
Expand Down Expand Up @@ -1879,6 +1879,18 @@ def test_enum_nonnative_column_copy_transfers_events(self):
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()

Expand All @@ -1889,6 +1901,16 @@ def test_boolean_column_copy_transfers_events(self):

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)
Expand Down

0 comments on commit 0737f45

Please sign in to comment.