]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Disable eager loads for exists()
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 6 Dec 2017 21:22:31 +0000 (16:22 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 6 Dec 2017 21:23:44 +0000 (16:23 -0500)
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

doc/build/changelog/unreleased_12/4032.rst [new file with mode: 0644]
lib/sqlalchemy/orm/query.py
test/orm/test_query.py

diff --git a/doc/build/changelog/unreleased_12/4032.rst b/doc/build/changelog/unreleased_12/4032.rst
new file mode 100644 (file)
index 0000000..869be35
--- /dev/null
@@ -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.
\ No newline at end of file
index b3bb5302f7891f1e9926b732e7fa9f7c12ec9418..209bb6d6a50adf00d78df26416bfbaf70b918b7e 100644 (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):
index 19adf8983ff5703128feceb045d0783939876f3d..3b91f5ffaf749621c38384d66157d9ab2a68c955 100644 (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