self.columns[c.name] = c_copy
self.named_constraints = {}
self.unnamed_constraints = []
+ self.col_named_constraints = {}
self.indexes = {}
self.new_indexes = {}
+
for const in self.table.constraints:
if _is_type_bound(const):
continue
else:
self.unnamed_constraints.append(const)
+ if not self.reflected:
+ for col in self.table.c:
+ for const in col.constraints:
+ if const.name:
+ self.col_named_constraints[const.name] = (col, const)
+
for idx in self.table.indexes:
self.indexes[idx.name] = idx
if not const.name:
raise ValueError("Constraint must have a name")
try:
- const = self.named_constraints.pop(const.name)
+ if const.name in self.col_named_constraints:
+ col, const = self.col_named_constraints.pop(const.name)
+
+ for col_const in list(self.columns[col.name].constraints):
+ if col_const.name == const.name:
+ self.columns[col.name].constraints.remove(col_const)
+ else:
+ const = self.named_constraints.pop(const.name)
except KeyError:
if _is_type_bound(const):
# type-bound constraints are only included in the new
)
return ApplyBatchImpl(self.impl, t, table_args, table_kwargs, False)
+ def _named_ck_table_fixture(self, table_args=(), table_kwargs={}):
+ m = MetaData()
+ t = Table(
+ "tname",
+ m,
+ Column("id", Integer, primary_key=True),
+ Column("x", String()),
+ Column("y", Integer),
+ CheckConstraint("y > 5", name="ck1"),
+ )
+ return ApplyBatchImpl(self.impl, t, table_args, table_kwargs, False)
+
+ def _named_ck_col_fixture(self, table_args=(), table_kwargs={}):
+ m = MetaData()
+ t = Table(
+ "tname",
+ m,
+ Column("id", Integer, primary_key=True),
+ Column("x", String()),
+ Column("y", Integer, CheckConstraint("y > 5", name="ck1")),
+ )
+ return ApplyBatchImpl(self.impl, t, table_args, table_kwargs, False)
+
def _ix_fixture(self, table_args=(), table_kwargs={}):
m = MetaData()
t = Table(
ddl_not_contains="CONSTRAINT uq1 UNIQUE",
)
+ def test_add_ck(self):
+ impl = self._simple_fixture()
+ ck = self.op.schema_obj.check_constraint("ck1", "tname", "y > 5")
+
+ impl.add_constraint(ck)
+ self._assert_impl(
+ impl,
+ colnames=["id", "x", "y"],
+ ddl_contains="CONSTRAINT ck1 CHECK (y > 5)",
+ )
+
+ def test_drop_ck_table(self):
+ impl = self._named_ck_table_fixture()
+
+ ck = self.op.schema_obj.check_constraint("ck1", "tname", "y > 5")
+ impl.drop_constraint(ck)
+ self._assert_impl(
+ impl,
+ colnames=["id", "x", "y"],
+ ddl_not_contains="CONSTRAINT ck1 CHECK (y > 5)",
+ )
+
+ def test_drop_ck_col(self):
+ impl = self._named_ck_col_fixture()
+
+ ck = self.op.schema_obj.check_constraint("ck1", "tname", "y > 5")
+ impl.drop_constraint(ck)
+ self._assert_impl(
+ impl,
+ colnames=["id", "x", "y"],
+ ddl_not_contains="CONSTRAINT ck1 CHECK (y > 5)",
+ )
+
def test_create_index(self):
impl = self._simple_fixture()
ix = self.op.schema_obj.index("ix1", "tname", ["y"])