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
--- /dev/null
+.. 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.
\ No newline at end of file
# 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):
"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