From: Mike Bayer Date: Sun, 29 Apr 2007 20:30:55 +0000 (+0000) Subject: - fix to using distinct() or distinct=True in combination with X-Git-Tag: rel_0_3_7~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9fc5a1fe466e6eb6ba5beb664ef7efaceef62275;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - fix to using distinct() or distinct=True in combination with join() and similar --- diff --git a/CHANGES b/CHANGES index 934733d0b1..ea7e68d6c6 100644 --- a/CHANGES +++ b/CHANGES @@ -89,6 +89,8 @@ - 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. diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index edbd53fdca..355ba68f79 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -344,7 +344,7 @@ class Query(object): 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) diff --git a/test/orm/mapper.py b/test/orm/mapper.py index 08faf721f9..8a0f28c478 100644 --- a/test/orm/mapper.py +++ b/test/orm/mapper.py @@ -459,7 +459,7 @@ class MapperTest(MapperSuperTest): @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) @@ -467,7 +467,13 @@ class MapperTest(MapperSuperTest): 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 @@ -1098,7 +1104,9 @@ class LazyTest(MapperSuperTest): {'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"""