8 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: How to order by a fi...
FromSent OnAttachments
Mart27 Dec 2006 09:41 
Michael Bayer27 Dec 2006 11:26 
Mart28 Dec 2006 03:55 
Michael Bayer28 Dec 2006 08:44 
Michael Bayer28 Dec 2006 08:47 
Mart28 Dec 2006 12:58 
Mart28 Dec 2006 12:58 
Jonathan Ellis06 Jan 2007 22:10 
Subject:[sqlalchemy] Re: How to order by a field in a different table?
From:Michael Bayer (zzz@gmail.com)
Date:12/28/2006 08:44:44 AM
List:com.googlegroups.sqlalchemy

usually thats what people want for this particular question, since they want all the behavior to be abstracted through their relation()s. but since you have some actual SQL youd like, here goes:

straight select

s = select([forum_topic], forum_topic.c.id==forum_post.c.topic_id, group_by=[forum_topic.c.id], order_by=[desc(forum_post.c.created)])

through the ORM:

session.query(ForumTopic).select(forum_topic.c.id==forum_post.c.topic_id, group_by=[forum_topic.c.id], order_by=[desc(forum_post.c.created)])

if the GROUP BY doesnt work with the Query since i havent tested that it gets propigated through to the generated query lately, you can feed the full select into the query too:

result = session.query(ForumTopic).select(s)

or feed in its results:

result = session.query(ForumTopic).instances(s.execute())