Think about when related data is fetched from the database.
Using lazy='dynamic' returns a query object for related items, so you can filter or paginate before loading. This delays actual database access until you explicitly ask for data.
users = User.query.all() for user in users: print(user.posts)
Consider how many database queries happen inside the loop.
Without eager loading, accessing user.posts triggers a separate query per user, causing the N+1 query problem.
from sqlalchemy.orm import joinedload users = User.query.options(_____).all()
Check how to reference the relationship attribute correctly.
The correct syntax uses the class attribute User.posts inside joinedload(). Passing a string or undefined variable causes errors.
Think about how many queries happen inside the loop.
Accessing user.posts inside the loop without eager loading causes a separate query per user, leading to slow performance.
Remember what subqueryload does with related collections.
subqueryload loads all related comments for the filtered posts in one separate query, improving performance by avoiding N+1 queries.