Mike Bayer avatar Mike Bayer committed 865f78e

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

Comments (0)

Files changed (3)

           to query.options(), passed by name
           to query.with_parent().
 
+  - Fixed bug whereby mapper.order_by attribute would
+    be ignored in the "inner" query within a 
+    subquery eager load.  [ticket:2287].
+    Also in 0.6.9.
+
   - Identity map .discard() uses dict.pop(,None) 
     internally instead of "del" to avoid KeyError/warning 
     during a non-determinate gc teardown [ticket:2267]

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)
 

test/orm/test_subquery_relations.py

         l = q.order_by(sa.desc(User.id)).limit(2).offset(2).all()
         eq_(list(reversed(self.static.user_all_result[0:2])), l)
 
+    def test_mapper_order_by(self):
+        users, User, Address, addresses = (self.tables.users,
+                                self.classes.User,
+                                self.classes.Address,
+                                self.tables.addresses)
+
+        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])))
+
+
     def test_one_to_many_scalar(self):
         Address, addresses, users, User = (self.classes.Address,
                                 self.tables.addresses,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.