C. user parameter should be a dictionary, not an object
D. commit should be called before add
Solution
Step 1: Check method calls on db.session
The commit method must be called with parentheses: commit().
Step 2: Identify the missing parentheses error
Without parentheses, commit is just a reference, so changes are not saved.
Final Answer:
Missing parentheses after commit method call -> Option A
Quick Check:
commit() needs parentheses to execute [OK]
Hint: Always call commit() with parentheses [OK]
Common Mistakes:
Forgetting parentheses on commit
Using db.add instead of db.session.add
Calling commit before add
5. You want to implement a repository method that returns all users whose email ends with '@example.com'. Which of these is the best way to write it using SQLAlchemy in Flask?
hard
A. def get_example_users(self): return User.query.filter(User.email.contains('@example.com')).first()
B. def get_example_users(self): return User.query.filter_by(email='@example.com').all()
C. def get_example_users(self): return User.query.filter(User.email.endswith('@example.com')).all()
D. def get_example_users(self): return User.query.filter(User.email == '@example.com').all()
Solution
Step 1: Understand filtering by string suffix in SQLAlchemy
To filter emails ending with a string, use filter(User.email.endswith(...)).
Step 2: Check each option's correctness
def get_example_users(self): return User.query.filter(User.email.endswith('@example.com')).all() uses endswith and all() to get all matches, which is correct. def get_example_users(self): return User.query.filter_by(email='@example.com').all() uses filter_by with exact match, not suffix. def get_example_users(self): return User.query.filter(User.email.contains('@example.com')).first() uses contains but returns only first match. def get_example_users(self): return User.query.filter(User.email == '@example.com').all() uses equality check, not suffix.
Final Answer:
def get_example_users(self): return User.query.filter(User.email.endswith('@example.com')).all() -> Option C
Quick Check:
Use endswith() + all() for suffix filtering [OK]
Hint: Use endswith() for suffix filtering in queries [OK]
Common Mistakes:
Using filter_by for partial matches
Using contains but returning only first result
Using equality operator instead of endswith
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.