0
0
Flaskframework~20 mins

Model definition in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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))
A1
B0
C2
DRaises an AttributeError
Attempts:
2 left
💡 Hint
filter_by returns a list of matching records.
📝 Syntax
intermediate
2:00remaining
Which model definition will cause a syntax error?
Identify the option that will cause a syntax error when defining a Flask-SQLAlchemy model.
A
class Product(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
B
)eslaF=elballun ,)001(gnirtS.bd(nmuloC.bd = eman    
)eurT=yek_yramirp ,regetnI.bd(nmuloC.bd = di    
:)ledoM.bd(tcudorP ssalc
C
class Product(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
D
class Product(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=True)
Attempts:
2 left
💡 Hint
Check for missing commas between arguments.
state_output
advanced
2: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
A'new@example.com'
BRaises an AttributeError
CNone
D'test@example.com'
Attempts:
2 left
💡 Hint
Assigning a new value to an attribute updates it.
🔧 Debug
advanced
2: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))
AMissing primary key causes the error.
BDuplicate column name 'total' causes a conflict.
Cdb.Float is not a valid column type.
DNullable=False is required for all columns.
Attempts:
2 left
💡 Hint
Check if any column names are repeated.
🧠 Conceptual
expert
3: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.
A
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    books = db.Column(db.Integer, db.ForeignKey('book.id'))

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
B
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    books = db.relationship('Book')

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer)
C
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer)
D
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    books = db.relationship('Book', backref='author')

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
Attempts:
2 left
💡 Hint
One-to-many uses relationship on the 'one' side and ForeignKey on the 'many' side.