]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
The where method of exists now accepts multiple cluase.
authorFederico Caselli <cfederico87@gmail.com>
Sat, 4 Dec 2021 22:08:05 +0000 (23:08 +0100)
committerFederico Caselli <cfederico87@gmail.com>
Sat, 4 Dec 2021 22:09:38 +0000 (23:09 +0100)
Support multiple clause elements in the :meth:`_sql.Exists.where` method,
unifying the api with the on presented by a normal :func:`_sql.select`
construct.

Fixes: #7386
Change-Id: I5df20478008cd5167053d357cbfad8a641c62b44
(cherry picked from commit b2bc0c8e4138ccef4834a415f7be9012e1c6286e)

doc/build/changelog/unreleased_14/7386.rst [new file with mode: 0644]
lib/sqlalchemy/sql/selectable.py
test/sql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_14/7386.rst b/doc/build/changelog/unreleased_14/7386.rst
new file mode 100644 (file)
index 0000000..e344453
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: usecase, sql
+    :tickets: 7386
+
+    Support multiple clause elements in the :meth:`_sql.Exists.where` method,
+    unifying the api with the on presented by a normal :func:`_sql.select`
+    construct.
index 95fca267c65d7d34bb5c0dcbb8bc63ab802f20b0..587d55935016987866fee50362348caac9fc52ba 100644 (file)
@@ -6597,6 +6597,9 @@ class Exists(UnaryExpression):
 
     See :func:`_sql.exists` for a description of usage.
 
+    An ``EXISTS`` clase can also be construed from a :func:`_sql.select`
+    instance by calling :meth:`_sql.SelectBase.exists`.
+
     """
 
     _from_objects = []
@@ -6635,6 +6638,9 @@ class Exists(UnaryExpression):
 
             :ref:`tutorial_exists` - in the :term:`2.0 style` tutorial.
 
+            :meth:`_sql.SelectBase.exists` - method to transform a ``SELECT`` to an
+            ``EXISTS`` clause.
+
         """  # noqa E501
         if args and isinstance(args[0], (SelectBase, ScalarSelect)):
             s = args[0]
@@ -6749,7 +6755,7 @@ class Exists(UnaryExpression):
         e.element = self._regroup(lambda element: element.select_from(*froms))
         return e
 
-    def where(self, clause):
+    def where(self, *clause):
         """Return a new :func:`_expression.exists` construct with the
         given expression added to
         its WHERE clause, joined to the existing clause via AND, if any.
@@ -6762,7 +6768,7 @@ class Exists(UnaryExpression):
 
         """
         e = self._clone()
-        e.element = self._regroup(lambda element: element.where(clause))
+        e.element = self._regroup(lambda element: element.where(*clause))
         return e
 
 
index 23cf4eca3ab5b07b9cbf4eec0fafd252eb961fa2..eeb102162d636f748585b90f64033edf2e555f99 100644 (file)
@@ -1222,6 +1222,14 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
             "SELECT NOT (NOT (EXISTS (SELECT 1))) AS anon_1",
         )
 
+        self.assert_compile(
+            exists(42)
+            .select_from(table1)
+            .where(table1.c.name == "foo", table1.c.description == "bar"),
+            "EXISTS (SELECT 42 FROM mytable WHERE mytable.name = :name_1 "
+            "AND mytable.description = :description_1)",
+        )
+
     def test_exists_method(self):
         subq = (
             select(func.count(table2.c.otherid))