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:
Mike Bayer
2017-12-06 16:22:31 -05:00
parent 76b5981b41
commit 1cbaff643f
3 changed files with 31 additions and 1 deletions
+9
View File
@@ -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.
+2 -1
View File
@@ -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):
+20
View File
@@ -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