Hello,

Thanks, it helped.
Proper query for my case looks as follows:

        b1 = aliased(B)
        b2 = aliased(B)
        q = session.query(A.id, b1.id.label("b1_id"), b1.id.label("b2_id"))
        q = q.outerjoin(b1, sqlalchemy.and_(A.id == b1.a_id, b1.c_id == 66))
        q = q.outerjoin(b2, sqlalchemy.and_(A.id == b2.a_id, b2.c_id == 70))
        q = q.filter(sqlalchemy.or_(b1.id != None, b2.id != None))

Regards,
Godfryd

On Monday, March 25, 2013 6:22:30 PM UTC+1, Michael Bayer wrote:
>
>
> On Mar 25, 2013, at 1:54 AM, Michal Nowikowski 
> <[email protected]<javascript:>> 
> wrote:
>
> I got to the point where proper raw SQL query looks as follows:
>
> SELECT 
>   A.id AS a_id,
>   B_1.id AS b1_id, 
>   B_2.id AS b2_id,  
> FROM 
>   A
> LEFT OUTER JOIN   B AS B_1  ON A.id = B_1.a_id AND B_1.c_id = 66
> LEFT OUTER JOIN   B AS B_2  ON A.id = B_2.a_id AND B_2.c_id = 70
> WHERE
>   B_1.id is not NULL or 
>   B_2.id is not NULL;
>
> Now, do you know how to get this coded either in ORM query or SA SQL 
> expressions?
>
>
> the ORM tutorial has an example for joining to aliased tables that is 
> nearly identical in structure to this query:
>
> http://docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html#using-aliases
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to