From: Mike Bayer Date: Wed, 28 Sep 2011 13:12:55 +0000 (-0400) Subject: - Fixed bug whereby mapper.order_by attribute would X-Git-Tag: rel_0_6_9~24 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=290064ab235b4ce30b84394db5219f74b69736ea;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug whereby mapper.order_by attribute would be ignored in the "inner" query within a subquery eager load. [ticket:2287]. --- diff --git a/CHANGES b/CHANGES index b06899297e..6d27cc2e85 100644 --- a/CHANGES +++ b/CHANGES @@ -32,6 +32,10 @@ CHANGES table would convert the lead entity into the joined one inappropriately. [ticket:2234] + - Fixed bug whereby mapper.order_by attribute would + be ignored in the "inner" query within a + subquery eager load. [ticket:2287]. + - Fixed bug whereby if a mapped class redefined __hash__() or __eq__() to something non-standard, which is a supported use case diff --git a/lib/sqlalchemy/orm/strategies.py b/lib/sqlalchemy/orm/strategies.py index ac49d335d9..677b5f8621 100644 --- a/lib/sqlalchemy/orm/strategies.py +++ b/lib/sqlalchemy/orm/strategies.py @@ -776,6 +776,9 @@ class SubqueryLoader(AbstractRelationshipLoader): # with polymorphic loading ? q._set_entities(q._adapt_col_list(leftmost_attr)) + if q._order_by is False: + q._order_by = leftmost_mapper.order_by + # don't need ORDER BY if no limit/offset if q._limit is None and q._offset is None: q._order_by = None @@ -828,6 +831,7 @@ class SubqueryLoader(AbstractRelationshipLoader): getattr(parent_alias, self.parent._columntoproperty[c].key) for c in local_cols ] + q = q.order_by(*local_attr) q = q.add_columns(*local_attr) diff --git a/test/orm/test_subquery_relations.py b/test/orm/test_subquery_relations.py index f58ebc8c6c..2cca51f85c 100644 --- a/test/orm/test_subquery_relations.py +++ b/test/orm/test_subquery_relations.py @@ -545,6 +545,22 @@ class EagerTest(_fixtures.FixtureTest, testing.AssertsCompiledSQL): l = q.order_by(sa.desc(User.id)).limit(2).offset(2).all() eq_(list(reversed(self.static.user_all_result[0:2])), l) + @testing.resolve_artifact_names + def test_mapper_order_by(self): + + mapper(Address, addresses) + mapper(User, users, properties={ + 'addresses':relationship(Address, + lazy='subquery', + order_by=addresses.c.id), + },order_by=users.c.id.desc()) + + sess = create_session() + q = sess.query(User) + + l = q.limit(2).all() + eq_(l, list(reversed(self.static.user_address_result[2:4]))) + @testing.resolve_artifact_names def test_one_to_many_scalar(self): mapper(User, users, properties = dict(