Challenge - 5 Problems
ORM Mastery in Flask
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
How does ORM reduce manual SQL writing?
Which of the following best explains how ORM simplifies database access in Flask applications?
Attempts:
2 left
💡 Hint
Think about how ORM handles data between Python and the database.
✗ Incorrect
ORM (Object-Relational Mapping) lets developers work with Python objects instead of writing SQL queries directly. It translates Python code into SQL behind the scenes, reducing manual SQL writing.
❓ component_behavior
intermediate2:00remaining
What happens when you add a new object with ORM?
In Flask with SQLAlchemy ORM, what happens when you create a new object, add it to the session, and then commit?
Flask
new_user = User(name='Alice')
session.add(new_user)
session.commit()Attempts:
2 left
💡 Hint
Consider what session.commit() does in ORM.
✗ Incorrect
Adding an object to the session and committing saves it to the database as a new row. The ORM handles the SQL INSERT automatically.
📝 Syntax
advanced2:00remaining
Identify the correct ORM query syntax
Which option correctly queries all users with age greater than 30 using Flask SQLAlchemy ORM?
Flask
users = User.query.filter(User.age > 30).all()
Attempts:
2 left
💡 Hint
Remember the difference between filter and filter_by methods.
✗ Incorrect
filter() accepts expressions like User.age > 30, while filter_by() only accepts keyword arguments. Also, .all() returns all matching results.
🔧 Debug
advanced2:00remaining
Why does this ORM code raise an error?
Given the code below, which of the following is correct?
user = User(name='Bob')
session.add(user)
session.commit()
print(user.id)
Flask
user = User(name='Bob') session.add(user) session.commit() print(user.id)
Attempts:
2 left
💡 Hint
Think about how ORM handles primary keys after saving.
✗ Incorrect
After commit(), ORM updates the user object with the generated primary key id from the database, so user.id is accessible.
❓ state_output
expert2:00remaining
What is the output after modifying an ORM object before commit?
Consider this Flask SQLAlchemy code:
user = User(name='Carol', age=25)
session.add(user)
user.age = 26
session.commit()
print(user.age)
What will be printed?
Flask
user = User(name='Carol', age=25) session.add(user) user.age = 26 session.commit() print(user.age)
Attempts:
2 left
💡 Hint
Think about how ORM tracks changes before commit.
✗ Incorrect
ORM tracks changes to objects in the session. Changing user.age before commit updates the database and the object, so printing user.age shows 26.