What if your database could catch mistakes before they cause problems?
Why Column types and constraints in Flask? - Purpose & Use Cases
Imagine building a web app where you manually check every user input to make sure emails look right, numbers are in range, and text isn't too long before saving to your database.
Manually checking and cleaning data every time is slow, easy to forget, and causes bugs when invalid data sneaks in. It's like trying to catch every raindrop with a bucket instead of using an umbrella.
Using column types and constraints in your database models automatically enforces rules like data format and length. This means your app only saves valid data, reducing errors and saving you time.
if len(username) > 20: return 'Error: username too long' if '@' not in email: return 'Error: invalid email'
username = Column(String(20), nullable=False) email = Column(String(120), unique=True, nullable=False)
This lets your app trust the data it stores, making it safer, cleaner, and easier to work with.
Think of a signup form where the database won't accept duplicate emails or empty usernames because the column constraints catch these issues automatically.
Manual data checks are slow and error-prone.
Column types and constraints enforce data rules automatically.
This leads to safer and more reliable applications.
Practice
Solution
Step 1: Understand column types for text data
Text data of variable length is best stored using a string type that allows flexible length.Step 2: Match the correct SQLAlchemy type
db.Stringis designed for variable-length text, unlikedb.Integerordb.Boolean.Final Answer:
db.String -> Option AQuick Check:
Variable-length text = db.String [OK]
- Choosing db.Integer for text data
- Using db.Boolean for strings
- Confusing db.DateTime with text types
age in a Flask model?Solution
Step 1: Identify the correct type for age
Age should be stored as an integer, sodb.Integeris correct.Step 2: Set the nullable constraint
To make the column non-nullable, usenullable=False.Final Answer:
age = db.Column(db.Integer, nullable=False) -> Option DQuick Check:
Integer + non-nullable =age = db.Column(db.Integer, nullable=False)[OK]
- Setting nullable=True when non-null is needed
- Using wrong data type like String for age
- Confusing Boolean with Integer type
email = db.Column(db.String(120), unique=True, nullable=False)
What does the
unique=True constraint do?Solution
Step 1: Understand the unique constraint
Theunique=Trueconstraint ensures no two rows have the same value in this column.Step 2: Apply this to the email column
This means duplicate emails are not allowed, keeping each email distinct.Final Answer:
Prevents duplicate email values in the table -> Option AQuick Check:
unique=True means no duplicates [OK]
- Thinking unique=True allows duplicates
- Confusing unique with nullable
- Assuming unique changes data format
price = db.Column(db.String, nullable=False, unique='yes')
Solution
Step 1: Check the unique argument type
Theuniqueparameter expects a boolean value (TrueorFalse), not a string.Step 2: Identify the error in the code
Passingunique='yes'is invalid and will cause an error.Final Answer:
The unique argument should be a boolean, not a string -> Option BQuick Check:
unique must be True or False, not 'yes' [OK]
- Using string values for boolean parameters
- Thinking nullable and unique conflict
- Assuming price is reserved keyword
Solution
Step 1: Choose the correct type for phone numbers
Phone numbers often contain leading zeros, sodb.String(10)is better thandb.Integer.Step 2: Apply constraints for uniqueness and non-null
Useunique=Trueto prevent duplicates andnullable=Falseto disallow empty values.Final Answer:
phone = db.Column(db.String(10), unique=True, nullable=False) -> Option CQuick Check:
String(10) + unique + non-null =phone = db.Column(db.String(10), unique=True, nullable=False)[OK]
- Using Integer type losing leading zeros
- Allowing nullable=True when value is required
- Ignoring unique constraint for phone numbers
Ready to prove it?
Pass the test and this pattern moves unknown
