]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Don't use and_() inside of Query.filter_by
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 10 Apr 2019 15:51:27 +0000 (11:51 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 10 Apr 2019 15:56:49 +0000 (11:56 -0400)
Adjusted the :meth:`.Query.filter_by` method to not call :func:`.and()`
internally against multiple criteria, instead passing it off to
:meth:`.Query.filter` as a series of criteria, instead of a single criteria.
This allows :meth:`.Query.filter_by` to defer to :meth:`.Query.filter`'s
treatment of variable numbers of clauses, including the case where the list
is empty.  In this case, the :class:`.Query` object will not have a
``.whereclause``, which allows subsequent "no whereclause" methods like
:meth:`.Query.select_from` to behave consistently.

Fixes: #4606
Change-Id: Ifc8cdbf13accca2236068ef70114a7c35ab159ff

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

diff --git a/doc/build/changelog/unreleased_13/4606.rst b/doc/build/changelog/unreleased_13/4606.rst
new file mode 100644 (file)
index 0000000..6bdfa8a
--- /dev/null
@@ -0,0 +1,12 @@
+.. change::
+   :tags: bug, orm
+   :tickets: 4606
+
+   Adjusted the :meth:`.Query.filter_by` method to not call :func:`.and()`
+   internally against multiple criteria, instead passing it off to
+   :meth:`.Query.filter` as a series of criteria, instead of a single criteria.
+   This allows :meth:`.Query.filter_by` to defer to :meth:`.Query.filter`'s
+   treatment of variable numbers of clauses, including the case where the list
+   is empty.  In this case, the :class:`.Query` object will not have a
+   ``.whereclause``, which allows subsequent "no whereclause" methods like
+   :meth:`.Query.select_from` to behave consistently.
index 9544b7d117f0aaac18374ed997e97fda4849a63f..5d340654cd0c7e2a2254c0b987d6b0af910616e2 100644 (file)
@@ -1791,7 +1791,7 @@ class Query(object):
             _entity_descriptor(self._joinpoint_zero(), key) == value
             for key, value in kwargs.items()
         ]
-        return self.filter(sql.and_(*clauses))
+        return self.filter(*clauses)
 
     @_generative(_no_statement_condition, _no_limit_offset)
     def order_by(self, *criterion):
index 8f962d581180d04f429be3b72322c848a9140ecb..b31647a7251e68b5f28a71dfa9abd0d383f47bd5 100644 (file)
@@ -2880,6 +2880,15 @@ class FilterTest(QueryTest, AssertsCompiledSQL):
             checkparams={"email_address_1": "ed@ed.com", "name_1": "ed"},
         )
 
+    def test_empty_filters(self):
+        User = self.classes.User
+        sess = create_session()
+
+        q1 = sess.query(User)
+
+        is_(None, q1.filter().whereclause)
+        is_(None, q1.filter_by().whereclause)
+
     def test_filter_by_no_property(self):
         addresses = self.tables.addresses
         sess = create_session()