On Jun 27, 9:54 am, ltbarcly <[EMAIL PROTECTED]> wrote:
> I'm wondering how I would implement the following query.
>
> I have 3 tables A, B, C.  Each table has a many-to-many relationship
> to the other two through an association object, call these AB, AC,
> BC.
>
> If I want to select all the B's associated with a certain 'a', I can
> do:
>
> q = B.query()
> q = q.filter(A.c.b_id == ab.b_id)
> results = q.select()
>
> And I get a list of B's.  How do I get all the c's associated with the
> b's associated to a specific 'a', excluding those c's that are
> themselves related to the 'a' through AC?

lets think out loud...

select * from c join bc on <onclause> join b on <onclause> join ab on
<onclause> join a on <onclause> where a.id=<a id> and not
exists(select 1 from ac where ac.c_id=c.id)

so, let me also make "ac" inside the "exists" into an alias to avoid
correlation, and it should then be

ac_alias = ac.alias('ac_alias')
session.query(C).join(['bs',
'as']).filter(A.c.id==3).filter(~exists([1],
ac_alias.c.c_id==C.c.id)).list()


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