On Feb 10, 2010, at 10:17 AM, Andrija Zarić wrote: > Let's say I've got simple structure Order-->Item-->Detail. > > class Detail(Base): > ... > > class Order(Base): > ... > > class Item(Base): > ... > detail = relation(Detail, uselist=False, lazy=False) > order = relation(Order, uselist=False, backref='items') > > Of course I can specify order_by for Order.items by any columns from > Item, but is there a way I can order_by a column defined in Detail? > > I've naively tried something as backref('items', order_by=Detail.id), > but because Detail is anonymously joined to Item as e.g. 'details_1', > I've received ProgrammingError: invalid reference to FROM-clause > entry for table "details".
The Order.items collection doesn't have the ability to order by a remote column in the collection, unless you set the order_by to a subquery that joined out to the ultimate target you care about. Maybe, I haven't tried this, you could make an alternate (non primary) mapping to Item that was a join of Item and Detail, i.e. like: itemdetail = mapper(Item.__table__.join(Detail.__table__), non_primary=True) Order.items = relation(itemdetail, order_by=itemdetail.c.detail_id) I'm not 100% sure the non-primary mapper allowing additional attributes, and it might need to be a mapping of a select().select_from(join) and not the join directly, but if you have some time to experiment you might get something out of that. > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalch...@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.