0
0
Flaskframework~30 mins

Column types and constraints in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Column types and constraints
📖 Scenario: You are building a simple Flask app to manage a library's book collection. You need to create a database model to store book details with correct column types and constraints.
🎯 Goal: Create a Flask SQLAlchemy model called Book with columns that have specific types and constraints to store book information correctly.
📋 What You'll Learn
Create a Book model class inheriting from db.Model
Add a primary key column id with integer type
Add a title column with string type and maximum length 100, not nullable
Add an author column with string type and maximum length 50, not nullable
Add a published_year column with integer type
Add a isbn column with string type and maximum length 13, unique constraint
💡 Why This Matters
🌍 Real World
Defining database models with correct column types and constraints is essential for building reliable web applications that store and manage data accurately.
💼 Career
Understanding how to use Flask SQLAlchemy models with proper column types and constraints is a fundamental skill for backend web developers working with Python and Flask.
Progress0 / 4 steps
1
Set up the Book model with id column
Create a Flask SQLAlchemy model class called Book inheriting from db.Model. Add an integer column called id as the primary key.
Flask
Need a hint?

Define a class named Book that inherits from db.Model. Use db.Column with db.Integer and primary_key=True for the id column.

2
Add title and author columns with constraints
Add a title column with string type, maximum length 100, and not nullable. Add an author column with string type, maximum length 50, and not nullable.
Flask
Need a hint?

Use db.String with the length inside parentheses. Add nullable=False to make the columns required.

3
Add published_year column with integer type
Add a column called published_year with integer type to store the year the book was published.
Flask
Need a hint?

Use db.Column(db.Integer) to define the published_year column.

4
Add isbn column with unique constraint
Add a column called isbn with string type, maximum length 13, and a unique constraint to ensure no duplicate ISBNs.
Flask
Need a hint?

Use db.String(13) and add unique=True to the isbn column.