I want to query this structure:
A -> B
B -> C
B -> D
what's the syntax to do this with the Load interface, the documentation is not very clear on this (http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#sqlalchemy.orm.joinedload). All I see is how to do:
A -> B
B -> C
C -> D
Given the query:
query(A).options(joinedload(A.b).joinedload(B.c))
How do I go backward in the chain to specify the second relationship on B (B.d)?
Many to Many relationship between two tables is achieved by adding an association table such that it has two foreign keys - one from each table's primary key.
The loading of relationships falls into three categories; lazy loading, eager loading, and no loading. Lazy loading refers to objects are returned from a query without the related objects loaded at first.
This option, known as joinedload() , connects a JOIN (by default a LEFT OUTER join) to the statement and populates the scalar/collection from the same result set as that of the parent: sql>>> jack = session. query(User).\ ... options(joinedload('addresses')).\ ...
The sqlalchemy backref is one of the type keywords and it passed as the separate argument parameters which has to be used in the ORM mapping objects. It mainly includes the event listener on the configuration attributes with both directions of the user datas through explicitly handling the database relationships.
Just add another .options(...)
with a new relationship path:
q = (
session
.query(A)
.options(joinedload(A.b).joinedload(B.c))
.options(joinedload(A.b).joinedload(B.d))
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With