xliiv
xliiv

Reputation: 5629

How to map relation between one table and third table proxed by second table?

Let's have 3 tables A, B, C and relations between them.

A is many to one with B

B is one to many with C

How to write relation in sqlalchemy to have access to C from A, like: A.C[0].some_column

Upvotes: 1

Views: 112

Answers (1)

Neel
Neel

Reputation: 21243

In your example

A is many to one with B

B is one to many with C

So finally

A is many to many C is the last relationship if you want to access the C from A.

You have to create the relationship in each model to access directly.

For example

A.x is relationship with B
B.y is relationship with C

then you can access by A.x.y which will return you the list.

To access like A.x you can use secondaryjoin you can give query which will be access direct C from A.

For example

Class A:

    p = relationship(C,
             primaryjoin=A.x==B.y,
             secondaryjoin=B.y==C.z
             )

This might be work for you.

Upvotes: 3

Related Questions