Is there a more concise syntax for using sqlalchemy joinedload
to eager load items from more than one table that only relates to the query table by way of another intermediate table (or is there an alternative load syntax that is better for what I am trying to do)?
For example, using the familiar data structure of questions, answers, etc, is there a more succinct way than the example below to query a question, eager load the related answers, and eager load both the answer comments and the answer votes (assume, for this example, that both of these answer related items are contained in separate tables)?
from sqlalchemy.orm import joinedload
result = session.query(Question).\
options(
joinedload(Question.answers).
joinedload(Answer.comments)
).\
options(
joinedload(Question.answers).
joinedload(Answer.votes)
).\
filter(Question.id == '1').\
first()
I could not find any examples of loading multiple tables by way of an intermediate table at Relationship Loading Techniques (or anywhere else for that matter). I did attempt to include multiple sub-relationships inside a single joinedload
like...
result = session.query(Question).\
options(joinedload(Question.answers, Answer.comments, Answer.votes)).\
filter(Question.id == '1').\
first()
...but that approach, not surprisingly, just chains the joins.
Python Flask and SQLAlchemy ORM 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.
join() is used to alter the results of a query, joinedload() goes through great lengths to not alter the results of the query, and instead hide the effects of the rendered join to only allow for related objects to be present.
all() method. The Query object, when asked to return full entities, will deduplicate entries based on primary key, meaning if the same primary key value would appear in the results more than once, only one object of that primary key would be present.
You want Load.options()
Then your code would be something like:
result = session.query(Question).\
options(
joinedload(Question.answers).\
options(
joinedload(Answer.comments),
joinedload(Answer.votes)
)
).\
filter(Question.id == '1').\
first()
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