On Feb 7, 2008, at 2:06 PM, David Gardner wrote:

>
> This works, thanks, but how do I control the ordering?
> I want to order by the parent, then the child.
> If I do:
>
> grandchildren = session.query(Node).filter(Node.c.type==var1).\
> order_by(Node.name).\
> join('Parent', aliased=True).filter(Node.c.type==var2).\
> order_by(Node.name).\
> join('Parent',aliased=True,from_joinpoint=True).\
> filter(Node.uid==var3).all ()
>
> it sorts by child then parent. I can leave the first order_by out,
> but that still leaves the children unsorted. I tried writing the joins
> backwards in an attempt to control the ordering:

You can put the child order by at the end using reset_joinpoint():

grandchildren = session.query(Node).filter(Node.c.type==var1).\
join('Parent', aliased=True).filter(Node.c.type==var2).\
order_by(Node.name).\
join('Parent',aliased=True,from_joinpoint=True).\
filter(Node.uid==var3).reset_joinpoint().\
order_by(Node.name).\
all ()


if you are using trunk, you can alternatively use an actual Alias to  
construct the join and later reference that alias in the order_by():

parents = nodes.alias()
grandchildren = session.query(Node).filter(Node.c.type==var1).\
join([('Parent', parents)]).filter(Node.c.type==var2).\
join('Parent',aliased=True,from_joinpoint=True).\
filter(Node.uid==var3).
order_by([parents.c.name, Node.name]).\
all ()




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to