Bird
0
0

Given this repository method in Flask:

medium📝 state output Q13 of 15
Flask - Ecosystem and Patterns
Given this repository method in Flask:
def get_user_by_id(self, user_id):
    return User.query.filter_by(id=user_id).first()

What will repo.get_user_by_id(5) return if no user with ID 5 exists?
ANone
BRaises an exception
CAn empty list []
DA User object with id 5
Step-by-Step Solution
Solution:
  1. Step 1: Understand SQLAlchemy filter_by().first() behavior

    The first() method returns the first matching record or None if no match is found.
  2. Step 2: Apply to user_id 5 with no matching user

    Since no user with ID 5 exists, the method returns None, not a list or exception.
  3. Final Answer:

    None -> Option A
  4. Quick Check:

    filter_by().first() returns None if no match [OK]
Quick Trick: filter_by().first() returns None if no record found [OK]
Common Mistakes:
MISTAKES
  • Expecting an empty list instead of None
  • Thinking it raises an exception
  • Assuming it returns a dummy User object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes