External connection and ORM mapping in Sqlalchemy

I use ORM mapping 0.6 in Sqlalchemy eight

I have three tables (a, B and C) and there are no foreign keys between them

I'm trying to join tables a and B, and then connect with C I expect a field named tuple, fields a, B and C, and the C field is sometimes set to none.)

I can easily complete the first connection by selecting two tables

(session.query(A,B)
    .filter(A.some_field = B.some_other_field))

This gives me a tuple named "a" and "B"

Then I add an external connection to make it:

(session.query(A,B)
    .filter(A.some_field==B.some_other_field))
    .outerjoin((C,A.some_field==C.some_different_field))

The result is still only two tables I cannot access other fields of C (even if they exist)

What is the correct way to make a left outer join to access the fields of the rightmost table?

I'd rather not avoid basic SQL if I could avoid it - I'm trying to learn to use orm

Solution

This should work:

(session.query(A)
    .join(B,A.some_field == B.some_other_field)
    .outerjoin(C,A.some_field == C.some_different_field)
    .add_entity(B)
    .add_entity(C))
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>