From 3f2209776183193552cbc9516710deb6598fd6cd Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Fri, 1 Oct 2021 22:16:14 +0200 Subject: Warn when trying to execute a query object with a session. Passing a :class:`.Query` object to :meth:`_orm.Session.execute` is not the intended use of this object, and will now raise a deprecation warning. Fixes: #6284 Change-Id: I30a406d5a72335f9405f10f0a2030a32ccda41b9 --- lib/sqlalchemy/sql/coercions.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/coercions.py b/lib/sqlalchemy/sql/coercions.py index dce329352..f888bad4c 100644 --- a/lib/sqlalchemy/sql/coercions.py +++ b/lib/sqlalchemy/sql/coercions.py @@ -191,7 +191,6 @@ def expect( resolved = element else: resolved = element - if ( apply_propagate_attrs is not None and not apply_propagate_attrs._propagate_attrs @@ -843,6 +842,28 @@ class ReturnsRowsImpl(RoleImpl): class StatementImpl(_CoerceLiterals, RoleImpl): __slots__ = () + def _post_coercion(self, resolved, original_element, argname=None, **kw): + if resolved is not original_element and not isinstance( + original_element, util.string_types + ): + # use same method as Connection uses; this will later raise + # ObjectNotExecutableError + try: + original_element._execute_on_connection + except AttributeError: + util.warn_deprecated( + "Object %r should not be used directly in a SQL statement " + "context, such as passing to methods such as " + "session.execute(). This usage will be disallowed in a " + "future release. " + "Please use Core select() / update() / delete() etc. " + "with Session.execute() and other statement execution " + "methods." % original_element, + "1.4", + ) + + return resolved + def _implicit_coercions( self, original_element, resolved, argname=None, **kw ): -- cgit v1.2.1