Skip to content

Phase 5 - Databases (Flask-SQLAlchemy)

Most real apps need persistent data.

In Flask, the most common approach is:

  • Flask-SQLAlchemy (integration layer)
  • SQLAlchemy (ORM)
  • Flask-Migrate (migrations via Alembic)
  • What an ORM is and why it matters
  • Setting up Flask-SQLAlchemy
  • Defining models and data types
  • Creating tables with db.create_all() (dev only)
  • CRUD operations (create/read/update/delete)
  • Relationships (one-to-many, many-to-many)
  • Migrations (Flask-Migrate)
  • Raw SQL when you need it
  1. Introduction to ORMs
  2. Setting up Flask-SQLAlchemy
  3. Configuring Database URI
  4. Creating Database Models
  5. Primary Keys and Column Types
  6. Creating the Database (db.create_all)
  7. CRUD - Create Record
  8. CRUD - Read Record
  9. CRUD - Update Record
  10. CRUD - Delete Record
  11. One-to-Many Relationships
  12. Many-to-Many Relationships
  13. Database Migrations (Flask-Migrate)
  14. Executing Raw SQL

This phase exists to store data that outlives the request. It assumes forms producing validated values, and once it is done you can build a site whose data survives a restart.

diagram Diagram mermaid
sketch The track, and what each phase unlocks p5.js
Each phase assumes the one before it. Click a phase to see what you can build once it is done.
#page
1Introduction to ORMs
2Setting up Flask-SQLAlchemy
3Configuring Database URI
4Creating Database Models
5Primary Keys and Column Types
6Creating the Database (db.create_all)
7CRUD - Create Record
8CRUD - Read Record
9CRUD - Update Record
10CRUD - Delete Record
11One-to-Many Relationships
12Many-to-Many Relationships
13Database Migrations (Flask-Migrate)
14Executing Raw SQL

14 pages. They are ordered so that each one only uses ideas from the pages above it — reading out of order mostly works, but the examples assume what came before.

pch.quizTag pch.quizDefaultTitle
  1. What is the largest phase in the track about?

    pch.quizShowAnswer

    A — making data outlive the request, and modelling relationships between it — Fourteen pages cover setup, modelling, CRUD, relationships and migrations — the point at which an app stops being stateless.

  2. Why does Database Migrations appear near the end of this phase rather than next to db.create_all?

    pch.quizShowAnswer

    B — create_all is enough until the schema changes, and migrations are the answer to that specific later problem — create_all creates missing tables and never alters existing ones. Migrations exist for the moment a schema has to change after it has been deployed.

  3. What do the phase overview pages give you that the individual pages do not?

    pch.quizShowAnswer

    B — the reading order, what the phase assumes, and what you can build once it is done — Each overview lists its pages in sidebar order and names the phase's prerequisites and outcome, so you can tell whether you are ready for it.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading