0
0
Flaskframework~10 mins

Column types and constraints in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Column types and constraints
Define Model Class
Add Column with Type
Add Constraints (nullable, unique, primary_key)
Create Table in DB
Use Model for CRUD Operations
This flow shows how to define a database model in Flask with columns, specify their types and constraints, then create the table and use it.
Execution Sample
Flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(120), unique=True, nullable=False)
Defines a User model with an integer primary key and a unique, non-nullable email string column.
Execution Table
StepActionColumnTypeConstraintsResult
1Define id columnidIntegerprimary_key=TruePrimary key column created
2Define email columnemailString(120)unique=True, nullable=FalseUnique and required column created
3Create table in DB---Table 'user' created with columns and constraints
4Insert user with email---Success if email unique and not null
5Insert user with duplicate email---Fails due to unique constraint
6Insert user with null email---Fails due to nullable=False constraint
7Query user by id---Returns user with matching id
8End---Execution stops
💡 Execution stops after table creation and sample insertions demonstrate constraints enforcement.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
id columnundefinedInteger, primary_key=TrueInteger, primary_key=TrueDefined in DB tableUsed as PKUsed as PKUsed as PKDefined
email columnundefinedundefinedString(120), unique=True, nullable=FalseDefined in DB tableInserted unique emailDuplicate email errorNull email errorDefined
Key Moments - 3 Insights
Why does inserting a user with a duplicate email fail?
Because the email column has unique=True constraint, the database rejects duplicate values as shown in execution_table step 5.
What happens if we try to insert a user without an email?
The insert fails due to nullable=False constraint on the email column, preventing null values as shown in execution_table step 6.
Why is the id column special?
The id column is marked primary_key=True, so it uniquely identifies each row and is required, as shown in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what constraint is applied to the email column at step 2?
Aprimary_key=True
Bnullable=True
Cunique=True, nullable=False
Ddefault='user@example.com'
💡 Hint
Check the Constraints column in execution_table row for step 2.
At which step does inserting a user with a null email fail?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the row mentioning null email error in execution_table.
If we remove unique=True from email, what changes in the execution?
ADuplicate emails would be allowed
BEmails would have to be unique
CEmails would become nullable
DPrimary key would change
💡 Hint
Refer to the unique constraint effect shown in execution_table step 5.
Concept Snapshot
Define columns in Flask models using db.Column(type, constraints).
Common types: Integer, String(length).
Constraints: primary_key, unique, nullable.
Primary key uniquely identifies rows.
Unique prevents duplicates.
Nullable=False means value is required.
Full Transcript
In Flask, when defining database models, you create columns using db.Column and specify their data types like Integer or String. You can add constraints such as primary_key to mark a column as the unique identifier, unique to prevent duplicate values, and nullable to control if the column can be empty. For example, an id column with primary_key=True uniquely identifies each record. An email column with unique=True and nullable=False means every user must have a unique email address. When the table is created, these constraints are enforced by the database. Trying to insert duplicate or missing emails will cause errors. This visual trace shows how columns and constraints are defined, how the table is created, and how these rules affect inserting data.