]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- query.get() and query.load() do not take existing filter or other
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 6 Dec 2007 19:15:04 +0000 (19:15 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 6 Dec 2007 19:15:04 +0000 (19:15 +0000)
criterion into account; these methods *always* look up the given id
in the database or return the current instance from the identity map,
disregarding any existing filter, join, group_by or other criterion
which has been configured. [ticket:893]

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

diff --git a/CHANGES b/CHANGES
index 791d4d6ef20c8c8d84fb38fc462eecf4e7eb50bb..d73ea69f08c21dd19f8c2bed399f1f0353eddd91 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -43,7 +43,13 @@ CHANGES
      convenient than using dynamic relations in some cases; for those who 
      have not, you might notice your apps using a lot fewer queries than
      before in some situations. [ticket:871]
-     
+
+   - query.get() and query.load() do not take existing filter or other
+     criterion into account; these methods *always* look up the given id
+     in the database or return the current instance from the identity map, 
+     disregarding any existing filter, join, group_by or other criterion
+     which has been configured. [ticket:893]
+          
    - new synonym() behavior: an attribute will be placed on the mapped
      class, if one does not exist already, in all cases. if a property
      already exists on the class, the synonym will decorate the property
index 054ac04e740faf3bf1a8189ba997bd87eea71c84..eb0d49147c0ecb7d9fdfa173e6f8de494213d93c 100644 (file)
@@ -52,6 +52,15 @@ class Query(object):
         self._primary_adapter=None
         self._only_load_props = None
         self._refresh_instance = None
+    
+    def _no_criterion(self):
+        q = self._clone()
+        q._from_obj = [self.table]
+        q._alias_ids = {}
+        q._joinpoint = self.mapper
+        q._statement = q._aliases = q._criterion = None
+        q._order_by = q._group_by = q._distinct = False
+        return q
         
     def _clone(self):
         q = Query.__new__(Query)
@@ -757,8 +766,8 @@ class Query(object):
             ident = util.to_list(ident)
 
         q = self
-        
         if ident is not None:
+            q = q._no_criterion()
             params = {}
             (_get_clause, _get_params) = self.select_mapper._get_clause
             q = q.filter(_get_clause)
index f1b40e0b1ac6cb139889993e99039b623d880893..158576f1766edce0ed5f0af0c4c6ce5dba9c99aa 100644 (file)
@@ -60,7 +60,33 @@ class GetTest(QueryTest):
         s.clear()
         u2 = s.query(User).get(7)
         assert u is not u2
+    
+    def test_no_criterion(self):
+        """test that get()/load() does not use preexisting filter/etc. criterion"""
+        
+        s = create_session()
+        
+        assert s.query(User).filter(User.id==7).get(19) is None
+
+        u = s.query(User).get(7)
+        assert s.query(User).filter(User.id==9).get(7) is u
+        s.clear()
+        assert s.query(User).filter(User.id==9).get(7).id == u.id
 
+        # user 10 has no addresses
+        u = s.query(User).get(10)
+        assert s.query(User).join('addresses').get(10) is u
+        s.clear()
+        assert s.query(User).join('addresses').get(10).id == u.id
+        
+        u = s.query(User).get(7)
+        assert s.query(User).join('addresses').filter(Address.user_id==8).filter(User.id==7).first() is None
+        assert s.query(User).join('addresses').filter(Address.user_id==8).get(7) is u
+        s.clear()
+        assert s.query(User).join('addresses').filter(Address.user_id==8).get(7).id == u.id
+        
+        assert s.query(User).join('addresses').filter(Address.user_id==8).load(7).id == u.id
+        
     def test_unique_param_names(self):
         class SomeUser(object):
             pass