From: Federico Caselli Date: Tue, 16 Dec 2025 20:30:47 +0000 (+0100) Subject: Avoid deprecation warning in add/drop constraint X-Git-Tag: rel_1_18_0~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c94816e864f6d89842ebf248cf116031287d506e;p=thirdparty%2Fsqlalchemy%2Falembic.git Avoid deprecation warning in add/drop constraint Ensure that alembic is compatible with the changes added in https://github.com/sqlalchemy/sqlalchemy/issues/13006 by explicitly setting isolate_from_table=True in sqlalchemy 2.1 Change-Id: I0dd3b2aa2932ca11fbde113fe4d92306fb1f2bbb --- diff --git a/alembic/ddl/impl.py b/alembic/ddl/impl.py index d352f12e..4e3f29ae 100644 --- a/alembic/ddl/impl.py +++ b/alembic/ddl/impl.py @@ -399,11 +399,15 @@ class DefaultImpl(metaclass=ImplMeta): ) ) - def add_constraint(self, const: Any) -> None: + def add_constraint(self, const: Any, **kw: Any) -> None: if const._create_rule is None or const._create_rule(self): - self._exec(schema.AddConstraint(const)) + if sqla_compat.sqla_2_1: + kw.setdefault("isolate_from_table", True) + self._exec(schema.AddConstraint(const, **kw)) def drop_constraint(self, const: Constraint, **kw: Any) -> None: + if sqla_compat.sqla_2_1: + kw.setdefault("isolate_from_table", True) self._exec(schema.DropConstraint(const, **kw)) def rename_table( diff --git a/alembic/ddl/sqlite.py b/alembic/ddl/sqlite.py index 5f141330..c260d53f 100644 --- a/alembic/ddl/sqlite.py +++ b/alembic/ddl/sqlite.py @@ -74,7 +74,7 @@ class SQLiteImpl(DefaultImpl): else: return False - def add_constraint(self, const: Constraint): + def add_constraint(self, const: Constraint, **kw: Any): # attempt to distinguish between an # auto-gen constraint and an explicit one if const._create_rule is None: diff --git a/docs/build/unreleased/constraint_deprecation.rst b/docs/build/unreleased/constraint_deprecation.rst new file mode 100644 index 00000000..c156dcf5 --- /dev/null +++ b/docs/build/unreleased/constraint_deprecation.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: usecase + + Avoid deprecation warning in add/drop constraint added in SQLAlchemy 2.1. + Ensure that alembic is compatible with the changes added in + https://github.com/sqlalchemy/sqlalchemy/issues/13006 + by explicitly setting ``isolate_from_table=True`` when running with + SQLAlchemy 2.1 or greater.