Commits

Mike Bayer committed 3314790

- fix to using distinct() or distinct=True in combination with
join() and similar

Comments (0)

Files changed (3)

     - added generative versions of aggregates, i.e. sum(), avg(), etc.
       to query. used via query.apply_max(), apply_sum(), etc. 
       #552
+    - fix to using distinct() or distinct=True in combination with 
+      join() and similar
     - corresponding to label/bindparam name generataion, eager loaders 
       generate deterministic names for the aliases they create using 
       md5 hashes.

lib/sqlalchemy/orm/query.py

         if self.table not in alltables:
             from_obj.append(self.table)
         if self._nestable(**kwargs):
-            s = sql.select([self.table], whereclause, **kwargs).alias('getcount').count()
+            s = sql.select([self.table], whereclause, from_obj=from_obj, **kwargs).alias('getcount').count()
         else:
             primary_key = self.primary_key_columns
             s = sql.select([sql.func.count(list(primary_key)[0])], whereclause, from_obj=from_obj, **kwargs)

test/orm/mapper.py

         
     @testbase.unsupported('firebird') 
     def testcount(self):
-        """test the count function on Query
+        """test the count function on Query.
         
         (why doesnt this work on firebird?)"""
         mapper(User, users)
         self.assert_(q.count()==3)
         self.assert_(q.count(users.c.user_id.in_(8,9))==2)
         self.assert_(q.count_by(user_name='fred')==1)
-            
+
+    def testmanytomany_count(self):
+        mapper(Item, orderitems, properties = dict(
+                keywords = relation(mapper(Keyword, keywords), itemkeywords, lazy = True),
+            ))
+        q = create_session().query(Item)
+        assert q.join('keywords').distinct().count(Keyword.c.name=="red") == 2
 
     def testoverride(self):
         # assert that overriding a column raises an error
             {'item_id' : 1, 'keywords' : (Keyword, [{'keyword_id' : 2}, {'keyword_id' : 4}, {'keyword_id' : 6}])},
             {'item_id' : 2, 'keywords' : (Keyword, [{'keyword_id' : 2}, {'keyword_id' : 5}, {'keyword_id' : 7}])},
         )
+    
 
+        
 class EagerTest(MapperSuperTest):
     def testbasic(self):
         """tests a basic one-to-many eager load"""