--- /dev/null
+.. change::
+ :tags: orm
+ :tickets: 6284
+
+ 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.
resolved = element
else:
resolved = element
-
if (
apply_propagate_attrs is not None
and not apply_propagate_attrs._propagate_attrs
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
):
q = sess.query(User).filter(User.id == 7).set_cache_key("user7")
eq_(
- sess.execute(q).all(),
+ sess.execute(q.statement).all(),
[(User(id=7, addresses=[Address(id=1)]),)],
)
eq_(list(q.cache), ["user7"])
eq_(
- sess.execute(q).all(),
+ sess.execute(q.statement).all(),
[(User(id=7, addresses=[Address(id=1)]),)],
)
@testing.combinations(
(
- lambda session, Address: session.query(Address),
+ lambda session, Address: session.query(Address).statement,
lambda Address: {"mapper": inspect(Address), "clause": mock.ANY},
"e2",
),
).compare(q1.selectable)
)
+ @testing.combinations(("session",), ("connection",), argnames="executor")
+ @testing.combinations(
+ ("execute",), ("scalars",), ("scalar",), argnames="method"
+ )
+ def test_no_query_in_execute(self, executor, method, connection):
+ # even though this test is testing deprecations, these deprecations
+ # become errors when removed so we dont want to remove this test,
+ # just update it
+
+ if executor == "session":
+ exec_obj = Session(connection)
+ else:
+ exec_obj = connection
+
+ meth = getattr(exec_obj, method)
+
+ q = Session().query(literal_column("1"))
+
+ if executor == "session":
+ with testing.expect_deprecated(
+ r"Object .*Query.* should not be used directly in a "
+ r"SQL statement context"
+ ):
+ meth(q)
+ else:
+ with testing.expect_raises_message(
+ sa_exc.ObjectNotExecutableError, "Not an executable object"
+ ):
+ meth(q)
+
class OnlyReturnTuplesTest(QueryTest):
def test_single_entity_false(self):
q = testing.resolve_lambda(test_case, **locals())
- row = s.execute(q.order_by(User.id)).first()
+ if isinstance(q, Query):
+ row = q.first()
+ else:
+ row = s.execute(q.order_by(User.id)).first()
assert "jack" in row
@testing.combinations(