Commits

Mike Bayer committed 86caf44

when Query does the "nested select" thing, it copies the ORDER BY to be placed on the "outer" select, so that eager loaders modify only the outer one and not the inner one

Comments (0)

Files changed (3)

 otherwise still requires you explicitly remove the instance from 
 the previous Session.
 - fixes to mapper compilation, checking for more error conditions
+- small fix to eager loading combined with ordering/limit/offset
 
 0.2.3
 - overhaul to mapper compilation to be deferred.  this allows mappers

lib/sqlalchemy/orm/query.py

             statement = sql.select([], sql.and_(*crit), from_obj=[self.table], use_labels=True)
  #           raise "OK statement", str(statement)
             if order_by:
+                # copy the order_by, since eager loaders will modify it, and we want the
+                # "inner" order_by to remain untouched
+                # see test/orm/mapper.py EagerTest.testmorelimit
+                order_by = [o.copy_container() for o in util.to_list(order_by)]
                 statement.order_by(*util.to_list(order_by))
         else:
             from_obj.append(self.table)

test/orm/mapper.py

         l = q.select((Item.c.item_name=='item 2') | (Item.c.item_name=='item 5') | (Item.c.item_name=='item 3'), order_by=[Item.c.item_id], limit=2)        
         self.assert_result(l, Item, *[item_keyword_result[1], item_keyword_result[2]])
         
+    def testmorelimit(self):
+        """tests that the ORDER BY doesnt get clobbered with a nested eager load, when the ORDER BY
+        is an expression.  requires the copying of the order by clause in query.compile()"""
+        ordermapper = mapper(Order, orders, properties = dict(
+                items = relation(mapper(Item, orderitems), lazy = False)
+            ))
+
+        m = mapper(User, users, properties = dict(
+            addresses = relation(mapper(Address, addresses), lazy = False),
+            orders = relation(ordermapper, primaryjoin = users.c.user_id==orders.c.user_id, lazy = False),
+        ))
+        sess = create_session()
+        q = sess.query(m)
         
+        l = q.select(q.join_to('orders'), order_by=desc(orders.c.user_id), limit=2, offset=1)
+        self.assert_result(l, User, *(user_all_result[2], user_all_result[0]))
         
     def testonetoone(self):
         m = mapper(User, users, properties = dict(