On May 30, 2009, at 6:57 AM, klaus wrote:
> > Let's assume there is a foreign key A.fk -> B.id from table A to table > B, and these tables and columns are mapped to corresponding classes. > Then let's translate the foreign key to a reference A.ref -> B via a > relation. > > We can easily join B onto A: session.query(A).join("ref") or > session.query(A).join(A.ref). > > Joining in the opposite direction is much uglier: > session.query(B).join > ((A, A.fk == B.id)). It seems to need the basic foreign key column fk > (if it is mapped). Is there a way to express this using only the > relation? Something like session.query(B).join((A, A.ref == B)) or any > notation like session.query(B).join((A, A.ref == B.self)) perhaps? Usually you'd just have a backref so that you can spell out the join in either direction using a relation name, which is the most direct way of achieving this. I think this makes sense since you are essentially configuring a "canned" join condition at the mapper level. There is also a lesser used option which is query(B).with_parent(<somea>) but that is not as flexible as a full join. It used to be the case that you could use the same relation in both directions - i.e. query(B).join((A, A.ref)) and it would often "work", but in recent releases I put an explicit check against that since it was preventing me from fixing some other bad behavior in join(). --~--~---------~--~----~------------~-------~--~----~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---