mirror of
https://github.com/sqlalchemy/sqlalchemy.git
synced 2026-05-28 03:26:01 -04:00
Disable eager loads for exists()
The :meth:`.Query.exists` method will now disable eager loaders for when the query is rendered. Previously, joined-eager load joins would be rendered unnecessarily as well as subquery eager load queries would be needlessly generated. The new behavior matches that of the :meth:`.Query.subquery` method. Fixes: #4032 Change-Id: Iacafc76aa9ae0b71928037fa9637e85ad434ee3a
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
.. change::
|
||||
:tags: bug, orm
|
||||
:tickets: 4032
|
||||
|
||||
The :meth:`.Query.exists` method will now disable eager loaders for when
|
||||
the query is rendered. Previously, joined-eager load joins would be rendered
|
||||
unnecessarily as well as subquery eager load queries would be needlessly
|
||||
generated. The new behavior matches that of the :meth:`.Query.subquery`
|
||||
method.
|
||||
@@ -3066,7 +3066,8 @@ class Query(object):
|
||||
# omitting the FROM clause from a query(X) (#2818);
|
||||
# .with_only_columns() after we have a core select() so that
|
||||
# we get just "SELECT 1" without any entities.
|
||||
return sql.exists(self.add_columns('1').with_labels().
|
||||
return sql.exists(self.enable_eagerloads(False).add_columns('1').
|
||||
with_labels().
|
||||
statement.with_only_columns([1]))
|
||||
|
||||
def count(self):
|
||||
|
||||
@@ -1489,6 +1489,26 @@ class ExpressionTest(QueryTest, AssertsCompiledSQL):
|
||||
"FROM users WHERE users.id = :id_1)"
|
||||
)
|
||||
|
||||
def test_subquery_no_eagerloads(self):
|
||||
User = self.classes.User
|
||||
s = Session()
|
||||
|
||||
self.assert_compile(
|
||||
s.query(User).options(joinedload(User.addresses)).subquery(),
|
||||
"SELECT users.id, users.name FROM users"
|
||||
)
|
||||
|
||||
def test_exists_no_eagerloads(self):
|
||||
User = self.classes.User
|
||||
s = Session()
|
||||
|
||||
self.assert_compile(
|
||||
s.query(
|
||||
s.query(User).options(joinedload(User.addresses)).exists()
|
||||
),
|
||||
"SELECT EXISTS (SELECT 1 FROM users) AS anon_1"
|
||||
)
|
||||
|
||||
def test_named_subquery(self):
|
||||
User = self.classes.User
|
||||
|
||||
|
||||
Reference in New Issue
Block a user