for c in columns
if isinstance(c, Column)
]
+ # these flags have already added their UniqueConstraint /
+ # Index objects to the table, so flip them off here.
+ # SQLAlchemy tometadata() avoids this instead by preserving the
+ # flags and skipping the constraints that have _type_bound on them,
+ # but for a migration we'd rather list out the constraints
+ # explicitly.
+ _constraints_included = kw.pop("_constraints_included", False)
+ if _constraints_included:
+ for c in cols:
+ c.unique = c.index = False
+
t = sa_schema.Table(name, m, *cols, **kw)
constraints = [
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import Numeric
+from sqlalchemy import PrimaryKeyConstraint
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import UniqueConstraint
eq_(diffs[0][0], "add_table")
eq_(len(diffs), 1)
- assert UniqueConstraint in set(
- type(c) for c in diffs[0][1].constraints
+
+ # checking for dupes also
+ eq_(
+ sorted(
+ [type(cons) for cons in diffs[0][1].constraints],
+ key=lambda c: c.__name__,
+ ),
+ [PrimaryKeyConstraint, UniqueConstraint],
+ )
+
+ @config.requirements.reflects_unique_constraints_unambiguously
+ def test_dont_add_uq_on_reverse_table_drop(self):
+ m1 = MetaData()
+ m2 = MetaData()
+ Table("no_uq", m1, Column("x", String(50), unique=True))
+ diffs = self._fixture(m1, m2)
+
+ eq_(diffs[0][0], "remove_table")
+ eq_(len(diffs), 1)
+
+ # because the drop comes from reflection, the "unique=True" flag
+ # is lost in any case.
+ eq_(
+ sorted(
+ [type(cons) for cons in diffs[0][1].constraints],
+ key=lambda c: c.__name__,
+ ),
+ [PrimaryKeyConstraint, UniqueConstraint],
)
def test_add_uq_ix_on_table_create(self):
assert UniqueConstraint not in set(
type(c) for c in diffs[0][1].constraints
)
+
eq_(diffs[1][0], "add_index")
- eq_(diffs[1][1].unique, True)
+ d_table = diffs[0][1]
+ d_idx = diffs[1][1]
+ eq_(d_idx.unique, True)
+
+ # check for dupes
+ eq_(len(diffs), 2)
+ assert not d_table.indexes
def test_add_ix_on_table_create(self):
m1 = MetaData()
"unique constraint reflection disabled for this suite"
)
+ def test_dont_add_uq_on_reverse_table_drop(self):
+ config.skip_test(
+ "unique constraint reflection disabled for this suite"
+ )
+
def test_unique_not_reported(self):
m1 = MetaData()
Table(