On Jun 18, 2008, at 11:17 AM, Arun Kumar PG wrote:

> one more thing here, i noticed now that the query formed by sa when  
> we do an eager load has got some problems (may be i am not doing the  
> right thing)
>
> here's the problem
>
> i have entities A, B. where A -> B (1:N relationship)
>
> i form a query like this
>
> clauses = []
> clauses.append(A.c.col1 == 'xyz')
> clauses.append(B.c.col == 'xcv')
>
> qry = session.query(B).filter_by(*clauses)
> eager_qry = qry.options(sqlalchemy.eagerload('a')
> eager_qry.all()
>
> the sql shows:
>
> select ... from A, B left outer join A as alias on alias.key == B.key
>
> why is A included for join two times ? i understand eager load might  
> be creating the outer join but looks like because i am having a  
> clause on A, A  is included in the first join as well.
>
> what is the right way to use it so that i can get rid off first join  
> and eager load A.
>
> this is creating a huge result set.


the joins created by eager loading are insulated from anything you do  
with filter(), order_by(), etc.  This is so that your collections load  
properly even if criterion were applied that would otherwise limit the  
collection from loading fully.

See 
http://www.sqlalchemy.org/trac/wiki/FAQ#ImusinglazyFalsetocreateaJOINOUTERJOINandSQLAlchemyisnotconstructingthequerywhenItrytoaddaWHEREORDERBYLIMITetc.whichreliesupontheOUTERJOIN
 
  .

You have two options here:  either use an explicit join in your query,  
so that the Query works regardless of the eager loading being present  
or not.  Or, if you truly want the eager loaded collection to reflect  
the query criterion, instead of using "eagerload" you can use  
"contains_eager", as described in 
http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_relation_strategies_containseager
 
  .  Those are the 0.5 docs but the same technique applies to the most  
recent 0.4 version.



--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to