Bird
0
0

Given the code below, what will be the output when accessing user.posts if lazy='select' is used?

medium📝 component behavior Q13 of 15
Flask - Performance Optimization
Given the code below, what will be the output when accessing user.posts if lazy='select' is used?
user = session.query(User).first()
print(user.posts)
AA list of posts is loaded only when <code>user.posts</code> is accessed.
BAll posts are loaded immediately with the user query.
CAn error occurs because posts are not loaded.
DPosts are never loaded even when accessed.
Step-by-Step Solution
Solution:
  1. Step 1: Understand lazy='select' behavior

    Lazy loading with 'select' means related posts are loaded only when accessed, not with the initial query.
  2. Step 2: Analyze code behavior

    When user.posts is printed, a separate query fetches posts at that moment.
  3. Final Answer:

    A list of posts is loaded only when user.posts is accessed. -> Option A
  4. Quick Check:

    lazy='select' = load on access [OK]
Quick Trick: Lazy='select' loads related data only when accessed [OK]
Common Mistakes:
MISTAKES
  • Assuming posts load with user query
  • Expecting error due to missing posts
  • Thinking posts never load

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes