Bird
0
0

Examine this repository method in Flask:

medium📝 Debug Q6 of 15
Flask - Ecosystem and Patterns
Examine this repository method in Flask:
def save_product(self, product):
    db.session.add(product)
    db.session.commit

What is the issue with this code?
Adb.session.commit should be replaced with db.session.flush.
BThe add method should be called with parentheses.
CThe product should be converted to a dictionary before adding.
DThe commit method is not called; parentheses are missing after commit.
Step-by-Step Solution
Solution:
  1. Step 1: Review commit usage

    In SQLAlchemy, commit is a method and must be called with parentheses.
  2. Step 2: Identify error

    The code uses db.session.commit without parentheses, so commit is not executed.
  3. Final Answer:

    The commit method is not called; parentheses are missing after commit. -> Option D
  4. Quick Check:

    Always call commit() with parentheses [OK]
Quick Trick: commit must be called as a method with () [OK]
Common Mistakes:
MISTAKES
  • Forgetting parentheses on commit
  • Thinking add needs parentheses (it does)
  • Confusing commit with flush

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes