Since I am new to SA just want if that means that even if we have an eager
load on a 1:N relationships we should still do an explicit JOIN if the query
involves columns from both side of relations?
On 4/9/07, Michael Bayer <[EMAIL PROTECTED]> wrote:
>
>
> eagerly loaded relationships are not part of the main Query criterion/
> select. the eager loads are always tacked on secondary to the inner
> query. the main goal being that if you had lazy or eagerly loaded
> relationships, in both cases youd get the identical result. so any
> tables that you add to the Query criterion are completely distinct
> from their possible appearance in an eager loaded relationship (it
> has to be this way, otherwise eager loads would change the result of
> the query..eager loads are meant to be an optimization only). thats
> why the StoryStats' table is getting added in to the inner query.
>
> so the approaches to take are:
>
> 1. explicitly join against StoryStats:
>
> session.query(Story).join('storystatrelation').order_by
> (StoryStats.c.rating)
>
> 2. create whatever query you want and load its instances via instances
> ():
>
> s = story_table.outerjoin(story_stats_table).select
> (order_by=story_stats_table.c.rating)
> session.query(Story).options(contains_eager
> ('storystatrelation')).instances(s.execute())
>
> 3. a little less dynamic, specify order_by in the eagerly loaded
> relation() in the mapper setup, and specify None for the query
> ordering (this is more of a trick).
>
> mapper(Story, story_table, properties={
> 'storystatrelation':relation(StoryStats,
> story_stats_table,
> lazy=False, order_by=[story_stats_table.c.rating])
> })
>
> session.query(Story).order_by(None)
>
>
> On Apr 8, 2007, at 5:39 PM, Norjee wrote:
>
> >
> > It seems I don't understand how i can order the results of a query.
> > Assume that i have two object Story and StoryStats. Each Story has
> > one
> > StoryStats, mapped by ForeignKey. The relation is eagerloaded
> > (lazy=False)
> > (The actual model is a tad more complicated, but the idea is the
> > same)
> >
> > When i now try to select Stories, ordering by create_date goes fine,
> > e.g.
> > session.query(Story).order_by(Story.c.create_date)
> >
> >
> > But ordering by the realated StoryStats goes awry :/
> > session.query(Story).order_by(StoryStats.c.rating), only a singe
> > Story
> > is returned
> > Now errors are thrown however.
> >
> >
> > Is there something I'm missing here? (I know I probably could do
> > session.query(StoryStats).order_by(StoryStats.c.rating), but that
> > kind
> > of defeats the purpose as the ordering is dynamic)
> >
> >
> > >
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---