Hi I would like to understand how does sqlalchemy lazy loading works? Assuming I have this query
results = (
session.query(Parent).
options(lazyload(Parent.children)).
filter(Parent.id == 1).
all()
)
for parent in results:
logging.error(parent.children)
I want to know if I access the parent.children on the for loop will this create a new select statement? or is the record or parent.children already cached or something? I'm thinking of how this will affect the performance. I just want to most optimize way.
lazyload(). The relationship collection attribute is populated when first accessed, lazily. If on the other hand you'd wish to avoid the possible N+1 situation, you could for example use joinedload() instead in order to populate children in the same query.echo=True in your engine configuration.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