if const._create_rule is None or const._create_rule(self):
self._exec(schema.AddConstraint(const))
- def drop_constraint(self, const: Constraint) -> None:
- self._exec(schema.DropConstraint(const))
+ def drop_constraint(self, const: Constraint, **kw: Any) -> None:
+ self._exec(schema.DropConstraint(const, **kw))
def rename_table(
self,
def drop_constraint(
self,
const: Constraint,
+ **kw: Any,
) -> None:
if isinstance(const, schema.CheckConstraint) and _is_type_bound(const):
return
"SQLite migrations using a copy-and-move strategy."
)
- def drop_constraint(self, const: Constraint):
+ def drop_constraint(self, const: Constraint, **kw: Any):
if const._create_rule is None:
raise NotImplementedError(
"No support for ALTER of constraints in SQLite dialect. "
type_: Optional[str] = None,
*,
schema: Optional[str] = None,
+ if_exists: Optional[bool] = None,
) -> None:
r"""Drop a constraint of the given name, typically via DROP CONSTRAINT.
quoting of the schema outside of the default behavior, use
the SQLAlchemy construct
:class:`~sqlalchemy.sql.elements.quoted_name`.
+ :param if_exists: If True, adds IF EXISTS operator when
+ dropping the constraint
+
+ .. versionadded:: 1.15.3
"""
type_: Optional[str] = None,
*,
schema: Optional[str] = None,
+ if_exists: Optional[bool] = None,
) -> None:
r"""Drop a constraint of the given name, typically via DROP CONSTRAINT.
quoting of the schema outside of the default behavior, use
the SQLAlchemy construct
:class:`~sqlalchemy.sql.elements.quoted_name`.
+ :param if_exists: If True, adds IF EXISTS operator when
+ dropping the constraint
+
+ .. versionadded:: 1.15.3
""" # noqa: E501
...
type_: Optional[str] = None,
*,
schema: Optional[str] = None,
+ if_exists: Optional[bool] = None,
_reverse: Optional[AddConstraintOp] = None,
) -> None:
self.constraint_name = constraint_name
self.table_name = table_name
self.constraint_type = type_
self.schema = schema
+ self.if_exists = if_exists
self._reverse = _reverse
def reverse(self) -> AddConstraintOp:
type_: Optional[str] = None,
*,
schema: Optional[str] = None,
+ if_exists: Optional[bool] = None,
) -> None:
r"""Drop a constraint of the given name, typically via DROP CONSTRAINT.
quoting of the schema outside of the default behavior, use
the SQLAlchemy construct
:class:`~sqlalchemy.sql.elements.quoted_name`.
+ :param if_exists: If True, adds IF EXISTS operator when
+ dropping the constraint
+
+ .. versionadded:: 1.15.3
"""
- op = cls(constraint_name, table_name, type_=type_, schema=schema)
+ op = cls(
+ constraint_name,
+ table_name,
+ type_=type_,
+ schema=schema,
+ if_exists=if_exists,
+ )
return operations.invoke(op)
@classmethod
from . import ops
from .base import Operations
from ..util.sqla_compat import _copy
+from ..util.sqla_compat import sqla_2
if TYPE_CHECKING:
from sqlalchemy.sql.schema import Table
def drop_constraint(
operations: "Operations", operation: "ops.DropConstraintOp"
) -> None:
+ kw = {}
+ if operation.if_exists is not None:
+ if not sqla_2:
+ raise NotImplementedError("SQLAlchemy 2.0 required")
+ kw["if_exists"] = operation.if_exists
operations.impl.drop_constraint(
operations.schema_obj.generic_constraint(
operation.constraint_name,
operation.table_name,
operation.constraint_type,
schema=operation.schema,
- )
+ ),
+ **kw,
)
is_posix = os.name == "posix"
+py314 = sys.version_info >= (3, 14)
py313 = sys.version_info >= (3, 13)
py311 = sys.version_info >= (3, 11)
py310 = sys.version_info >= (3, 10)
--- /dev/null
+.. change::
+ :tags: usecase, operations
+ :tickets: 1650
+
+ Added :paramref:`.op.drop_constraint.if_exists` parameter to
+ :func:`.op.drop_constraint` which will render "DROP CONSTRAINT IF EXISTS".
+ Pull request courtesy Aaron Griffin.
)
version_high = exclusions.only_if(
- lambda _: not compat.py313, "python 3.13 does not work right now"
+ lambda _: not compat.py314, "python 3.14 does not work right now"
)
sqlalchemy = exclusions.only_if(
op.drop_constraint("foo_bar_bat", "t1", schema="foo")
context.assert_("ALTER TABLE foo.t1 DROP CONSTRAINT foo_bar_bat")
+ def test_drop_constraint_if_exists(self):
+ context = op_fixture()
+ if sqla_compat.sqla_2:
+ op.drop_constraint("foo_bar_bat", "t1", if_exists=True)
+ context.assert_(
+ "ALTER TABLE t1 DROP CONSTRAINT IF EXISTS foo_bar_bat"
+ )
+ else:
+ with expect_raises_message(
+ NotImplementedError, "SQLAlchemy 2.0 required"
+ ):
+ op.drop_constraint("foo_bar_bat", "t1", if_exists=True)
+
def test_create_index(self):
context = op_fixture()
op.create_index("ik_test", "t1", ["foo", "bar"])