0
0
Flaskframework~20 mins

Column types and constraints in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Column Types and Constraints Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Identify the correct SQLAlchemy column type for storing large text
Which SQLAlchemy column type is best suited for storing large text data in a Flask model?
Adb.String(255)
Bdb.Integer
Cdb.Boolean
Ddb.Text
Attempts:
2 left
💡 Hint
Think about which type can hold long paragraphs without a fixed length.
component_behavior
intermediate
2:00remaining
Effect of nullable constraint on a Flask model column
What happens if you set nullable=False on a column in a Flask SQLAlchemy model?
AThe column cannot store NULL values and must have a value.
BThe column can store NULL values.
CThe column automatically generates unique values.
DThe column is ignored during database creation.
Attempts:
2 left
💡 Hint
Think about whether the database allows empty or missing values in that column.
state_output
advanced
2:00remaining
Output of unique constraint violation in Flask SQLAlchemy
Given a Flask model with a column defined as email = db.Column(db.String(120), unique=True), what happens if you try to add two users with the same email?
Flask
user1 = User(email='test@example.com')
db.session.add(user1)
db.session.commit()

user2 = User(email='test@example.com')
db.session.add(user2)
db.session.commit()
ABoth users are added successfully with the same email.
BThe database ignores the unique constraint and stores both.
CThe second commit raises an IntegrityError due to unique constraint violation.
DThe second user silently overwrites the first user in the database.
Attempts:
2 left
💡 Hint
What does a unique constraint do when a duplicate value is inserted?
🔧 Debug
advanced
2:00remaining
Identify the error in this Flask SQLAlchemy column definition
What is wrong with this column definition?
age = db.Column(db.Integer, nullable='False')
Flask
age = db.Column(db.Integer, nullable='False')
Adb.Integer is not a valid column type.
Bnullable should be a boolean, not a string.
CThe column name 'age' is reserved and cannot be used.
DThere is no error; this code is correct.
Attempts:
2 left
💡 Hint
Check the data type expected for the nullable parameter.
🧠 Conceptual
expert
3:00remaining
Understanding composite primary keys in Flask SQLAlchemy
Which of the following correctly defines a composite primary key on columns user_id and order_id in a Flask SQLAlchemy model?
A
user_id = db.Column(db.Integer)
order_id = db.Column(db.Integer)
__table_args__ = (db.PrimaryKeyConstraint('user_id', 'order_id'),)
B
user_id = db.Column(db.Integer, primary_key=True)
order_id = db.Column(db.Integer, primary_key=True)
C
user_id = db.Column(db.Integer, unique=True)
order_id = db.Column(db.Integer, unique=True)
D
user_id = db.Column(db.Integer, nullable=False)
order_id = db.Column(db.Integer, nullable=False)
Attempts:
2 left
💡 Hint
Composite primary keys can be explicitly defined using __table_args__ with PrimaryKeyConstraint.