Challenge - 5 Problems
Flask Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask-SQLAlchemy model query?
Given the model and query below, what will be printed?
Flask
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) # Assume the database has these users: # User(id=1, username='alice') # User(id=2, username='bob') users = User.query.filter_by(username='alice').all() print(len(users))
Attempts:
2 left
💡 Hint
filter_by returns a list of matching records.
✗ Incorrect
The query filters users with username 'alice'. Since there is one such user, the list length is 1.
📝 Syntax
intermediate2:00remaining
Which model definition will cause a syntax error?
Identify the option that will cause a syntax error when defining a Flask-SQLAlchemy model.
Attempts:
2 left
💡 Hint
Check for missing commas between arguments.
✗ Incorrect
Option C is missing a comma between db.String(100) and nullable=False, causing a syntax error.
❓ state_output
advanced2:00remaining
What is the value of user.email after this code runs?
Consider this model and code snippet. What will be the value of user.email after execution?
Flask
class User(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), unique=True, nullable=False) user = User(email='test@example.com') user.email = 'new@example.com' user.email
Attempts:
2 left
💡 Hint
Assigning a new value to an attribute updates it.
✗ Incorrect
The attribute user.email is updated to 'new@example.com' before accessing it.
🔧 Debug
advanced2:00remaining
Why does this model raise an error when creating the table?
This model raises an error when running db.create_all(). What is the cause?
Flask
class Order(db.Model): id = db.Column(db.Integer, primary_key=True) total = db.Column(db.Float, nullable=False) total = db.Column(db.String(50))
Attempts:
2 left
💡 Hint
Check if any column names are repeated.
✗ Incorrect
The model defines two columns with the same name 'total', which causes a conflict.
🧠 Conceptual
expert3:00remaining
Which option correctly defines a one-to-many relationship in Flask-SQLAlchemy?
Select the option that correctly sets up a one-to-many relationship between Author and Book models.
Attempts:
2 left
💡 Hint
One-to-many uses relationship on the 'one' side and ForeignKey on the 'many' side.
✗ Incorrect
Option D correctly uses db.relationship on Author and db.ForeignKey on Book to link them.