On Sep 26, 2008, at 9:02 PM, adolfo wrote:

> Hello, and sorry if this issue is already covered.
> I have a table which makes foreign references to other table, and I  
> need to make a filter against that table multiple times.
>
> so I have:
>
> q1=session.query(Fuente).outerjoin(Fuente.PPersona,  
> aliased=True).outerjoin(Fuente.PPersona_como_fuente, aliased=True)
>
> i.e. Fuente makes more than one reference to table Persona, thru the  
> properties PPersona and PPersona_como_fuente.
>
> until here, it works fine.
>
> Now if I need to construct a filter against Persona, how do I make  
> explicit that I want to make a condition to a field of Persona
> as, for example, Fuente.PPersona.sexo   and not  
> Fuente.PPersona_como_fuente.sexo     ?

when you're dealing with the aliased=True approach, this is a  
"shortcut" approach that apples the *most recent* alias to subsequent  
filter expressions, such as:

q1=session.query(Fuente).outerjoin(Fuente.PPersona,  
aliased 
= 
True 
).filter(Persona.foo=='bar').outerjoin(Fuente.PPersona_como_fuente,  
aliased=True).filter(Persona.bar=='foo')

the first filter is against Fuente.PPersona, the second against  
Fuenty.PPersona_como_fuente.

Now, the "explicit" approach is to use aliases.  In 0.5, its like this:

p1 = aliased(Persona)
p2 = aliased(Persona)
q1=session.query(Fuente).outerjoin((p1,  
Fuente.PPersona)).outerjoin((p2, Fuente.PPersona_como_fuente))

you then filter using p1 or p2:

q.filter(p1.foo=='bar').filter(p2.bar=='foo')

0.4 doesn't have an ORM-level "aliased" construct, so this approach  
requires using Table objects (using the Table.alias() method,  
constructing the joins with the expression language and using  
query.select_from(thejoin)) when working with 0.4.


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