]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Calling query.order_by() or query.distinct() before calling
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 11 Mar 2010 23:04:57 +0000 (18:04 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 11 Mar 2010 23:04:57 +0000 (18:04 -0500)
query.select_from(), query.with_polymorphic(), or
query.from_statement() raises an exception now instead of
silently dropping those criterion. [ticket:1736]

CHANGES
lib/sqlalchemy/orm/query.py
test/orm/test_query.py

diff --git a/CHANGES b/CHANGES
index 00cb3384e3df5c8190b39b95fa2978e790c35d5e..101c0223bde5deb7b520b82b186d0c1cb2b25f31 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -103,6 +103,11 @@ CHANGES
     it also is implemented within merge() such that a SELECT
     won't be issued for an incoming instance with partially
     NULL primary key if the flag is False.  [ticket:1680]
+
+  - Calling query.order_by() or query.distinct() before calling
+    query.select_from(), query.with_polymorphic(), or
+    query.from_statement() raises an exception now instead of 
+    silently dropping those criterion. [ticket:1736]
     
 - sql
   - The most common result processors conversion function were
index 0f68e0a41d55a794a04a283a4862cb0b9a54d993..88945b9d078354b21e42bb4cf3cb2141d21da610 100644 (file)
@@ -281,12 +281,16 @@ class Query(object):
             equivs.update(ent.mapper._equivalent_columns)
         return equivs
 
+    def _get_condition(self):
+        self._order_by = self._distinct = False
+        return self._no_criterion_condition("get")
+        
     def _no_criterion_condition(self, meth):
         if not self._enable_assertions:
             return
         if self._criterion is not None or self._statement is not None or self._from_obj or \
                 self._limit is not None or self._offset is not None or \
-                self._group_by:
+                self._group_by or self._order_by or self._distinct:
             raise sa_exc.InvalidRequestError(
                                 "Query.%s() being called on a "
                                 "Query with existing criterion. " % meth)
@@ -1554,7 +1558,7 @@ class Query(object):
 
         if refresh_state is None:
             q = self._clone()
-            q._no_criterion_condition("get")
+            q._get_condition()
         else:
             q = self._clone()
 
index 1bc24a2eed2d95dbdf53efd75d88e4ac1c2a1e72..054d64d20411d487c67eae6844b36b0ae4a5fb6c 100644 (file)
@@ -283,6 +283,25 @@ class InvalidGenerationsTest(QueryTest):
         s = create_session()
         q = s.query(User)
         assert_raises(sa_exc.InvalidRequestError, q.add_column, object())
+    
+    def test_distinct(self):
+        """test that a distinct() call is not valid before 'clauseelement' conditions."""
+        
+        s = create_session()
+        q = s.query(User).distinct()
+        assert_raises(sa_exc.InvalidRequestError, q.select_from, User)
+        assert_raises(sa_exc.InvalidRequestError, q.from_statement, text("select * from table"))
+        assert_raises(sa_exc.InvalidRequestError, q.with_polymorphic, User)
+
+    def test_order_by(self):
+        """test that an order_by() call is not valid before 'clauseelement' conditions."""
+
+        s = create_session()
+        q = s.query(User).order_by(User.id)
+        assert_raises(sa_exc.InvalidRequestError, q.select_from, User)
+        assert_raises(sa_exc.InvalidRequestError, q.from_statement, text("select * from table"))
+        assert_raises(sa_exc.InvalidRequestError, q.with_polymorphic, User)
+        
         
     def test_mapper_zero(self):
         s = create_session()