__visit_name__ = "add_constraint"
- def __init__(self, element):
+ def __init__(
+ self,
+ element: Constraint,
+ *,
+ isolate_from_table: bool = True,
+ ):
+ """Construct a new :class:`.AddConstraint` construct.
+
+ :param element: a :class:`.Constraint` object
+
+ :param isolate_from_table: optional boolean, defaults to True. Has
+ the effect of the incoming constraint being isolated from being
+ included in a CREATE TABLE sequence when associated with a
+ :class:`.Table`.
+
+ .. versionadded:: 2.0.39 - added
+ :paramref:`.AddConstraint.isolate_from_table`, defaulting
+ to True. Previously, the behavior of this parameter was implicitly
+ turned on in all cases.
+
+ """
super().__init__(element)
- element._create_rule = util.portable_instancemethod(
- self._create_rule_disable
- )
+
+ if isolate_from_table:
+ element._create_rule = util.portable_instancemethod(
+ self._create_rule_disable
+ )
class DropConstraint(_DropBase):
__visit_name__ = "drop_constraint"
- def __init__(self, element, cascade=False, if_exists=False, **kw):
+ def __init__(
+ self,
+ element: Constraint,
+ *,
+ cascade: bool = False,
+ if_exists: bool = False,
+ isolate_from_table: bool = True,
+ **kw: Any,
+ ):
+ """Construct a new :class:`.DropConstraint` construct.
+
+ :param element: a :class:`.Constraint` object
+ :param cascade: optional boolean, indicates backend-specific
+ "CASCADE CONSTRAINT" directive should be rendered if available
+ :param if_exists: optional boolean, indicates backend-specific
+ "IF EXISTS" directive should be rendered if available
+ :param isolate_from_table: optional boolean, defaults to True. Has
+ the effect of the incoming constraint being isolated from being
+ included in a CREATE TABLE sequence when associated with a
+ :class:`.Table`.
+
+ .. versionadded:: 2.0.39 - added
+ :paramref:`.DropConstraint.isolate_from_table`, defaulting
+ to True. Previously, the behavior of this parameter was implicitly
+ turned on in all cases.
+
+ """
self.cascade = cascade
super().__init__(element, if_exists=if_exists, **kw)
- element._create_rule = util.portable_instancemethod(
- self._create_rule_disable
- )
+
+ if isolate_from_table:
+ element._create_rule = util.portable_instancemethod(
+ self._create_rule_disable
+ )
class SetTableComment(_CreateDropBase):
"CHECK (a < b) DEFERRABLE INITIALLY DEFERRED",
)
- def test_external_ck_constraint_cancels_internal(self):
+ @testing.variation("isolate", [True, False])
+ @testing.variation("type_", ["add", "drop"])
+ def test_external_ck_constraint_cancels_internal(
+ self, isolate: testing.Variation, type_: testing.Variation
+ ):
t, t2 = self._constraint_create_fixture()
constraint = CheckConstraint(
table=t,
)
- schema.AddConstraint(constraint)
-
- # once we make an AddConstraint,
- # inline compilation of the CONSTRAINT
- # is disabled
- self.assert_compile(
- schema.CreateTable(t),
- "CREATE TABLE tbl (a INTEGER, b INTEGER)",
- )
+ if type_.add:
+ cls = schema.AddConstraint
+ elif type_.drop:
+ cls = schema.DropConstraint
+ else:
+ type_.fail()
+
+ if not isolate:
+ cls(constraint, isolate_from_table=False)
+ self.assert_compile(
+ schema.CreateTable(t),
+ "CREATE TABLE tbl (a INTEGER, b INTEGER, "
+ "CONSTRAINT my_test_constraint CHECK (a < b) "
+ "DEFERRABLE INITIALLY DEFERRED)",
+ )
+ else:
+ cls(constraint)
+ self.assert_compile(
+ schema.CreateTable(t),
+ "CREATE TABLE tbl (a INTEGER, b INTEGER)",
+ )
def test_render_drop_constraint(self):
t, t2 = self._constraint_create_fixture()