Commits

Mike Bayer committed 8f606d2

- Fixed bug whereby mapper.order_by attribute would
be ignored in the "inner" query within a
subquery eager load. [ticket:2287].

  • Participants
  • Parent commits 3a1f6e2
  • Branches rel_0_6

Comments (0)

Files changed (3)

     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

File lib/sqlalchemy/orm/strategies.py

         # 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
             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)
 

File test/orm/test_subquery_relations.py

         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(
             address = relationship(mapper(Address, addresses),