Columns, Types, and Constraints
Pick column types and add nullable and unique.
Columns, Types, and Constraints is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Types Shape Your Data
Every column has a type that decides what it can hold and how the database stores it. Picking the right one keeps data clean. 🎯
Integer for Whole Numbers
Use db.Integer for counts, ids, and ages. It stores whole numbers efficiently and is the usual choice for primary keys.
age = db.Column(db.Integer)String Needs a Length
db.String(120) holds short text and you give it a max length. Use it for names, emails, and titles.
email = db.Column(db.String(120))Text for Long Content
When length is unknown or huge, reach for db.Text. It is ideal for article bodies, comments, and descriptions.
bio = db.Column(db.Text)Boolean, Float, and Dates
There is a type for most needs: db.Boolean for flags, db.Float for decimals, and db.DateTime for timestamps.
active = db.Column(db.Boolean)Constraints Enforce Rules
Beyond types, constraints tell the database what is allowed. They protect your data even when your code has a bug.
nullable Controls Emptiness
Set nullable=False to require a value. The database then refuses to save a row that leaves that column empty.
name = db.Column(db.String(80), nullable=False)unique Blocks Duplicates
Add unique=True so no two rows share that value. It is perfect for emails or usernames that must stay distinct.
email = db.Column(db.String(120), unique=True)Provide a default
A default fills in a value when you omit one. Great for flags, counters, or a created-at timestamp.
active = db.Column(db.Boolean, default=True)Index for Fast Lookups
Mark a column with index=True when you filter on it often. The index speeds up searches at a small storage cost.
username = db.Column(db.String(50), index=True)Stack Options Together
Options combine freely on one column. Here a field is required, unique, and indexed all at once for a solid email field.
email = db.Column(db.String(120), nullable=False, unique=True, index=True)Quick Check
You want to guarantee no two users share an email. Which option do you add?
Recap
You matched data to types and added constraints like nullable, unique, default, and index to keep your tables trustworthy. 🛡️
Frequently asked questions
Is the “Columns, Types, and Constraints” lesson free?
Yes — the full text of “Columns, Types, and Constraints” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Columns, Types, and Constraints”?
Pick column types and add nullable and unique. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Columns, Types, and Constraints” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Install and Configure the Extension
- Define Your First Model Class
- Columns, Types, and Constraints
- Create the Schema with create_all