Hello. Suppose I have the following mapped classes, A and B, that have two distinct M:N relationships, AB1 and AB2. If A.x is null, only relations in AB1 apply. If it is not null, only relations in AB2 apply. A also has 1:N relationship to C (one A can have more Cs). Finally, A is infact a joined table inheritance superclass with two subclasses, A1 and A2. I want to select all As for a given B via AB1 or AB2. I also want to prefetch A.cs of the results using joinedload. I use the code like this:
q1 = session.query(A).with_polymorphic([A1, A2]) q1 = q1.filter(exists().where(and_( A.x == None, AB1.a_id == A.id, AB1.b_id == b_id, # input argument )) q2 = session.query(A).with_polymorphic([A1, A2]) q2 = q2.filter(exists().where(and_( A.x != None, AB2.a_id == A.id, AB2.b_id == b_id, # input argument, )) q = q1.union_all(q2) q = q.options( joinedload(A.cs), ) return q This creates the following SQL: SELECT .... FROM ( SELECT... -- via AB1 UNION ALL SELECT ... -- via AB2 ) anon_1 LEFT OUTER JOIN c ON c.a_id = a.id -- error line This fails with a missing from clause error for table a. The attribute a.id is actually anon_1.a_id. What am I doing wrong? / How can I fix this? Thank you in advance, Ladislav Lenart -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.