Sitemap
Towards AI

We train AI Engineers. We deploy Enterprise AI. Join 100K+ AI practitioners at Towards AI Academy or reach out for help making your company AI native. towardsai.com

Member-only story

IX. FastAPI — Database Patterns: SQLModel & Alembic

7 min readJan 25, 2026

--

Hi! So far we have covered almost every key concept, but without a database nothing can be persisted, right? So, today I will walk you through two important Magic Libraries that make the database operations hustle free.

Press enter or click to view image in full size

You’ve built endpoints. You’ve added auth. You’ve written tests. But your data lives in a dictionary, or worse — it vanishes when the server restarts.

Time to connect to a real database. And in Python, that means making a choice: raw SQL, SQLAlchemy, or SQLModel? Sync or async? ORM or not?

Let’s cut through the confusion.

Press enter or click to view image in full size

SQLModel was created by the same author of FastAPI. It combines:

  • Pydantic for validation and serialization
  • SQLAlchemy for database operations

One model does both jobs.

Let’s install before getting started

pip install sqlmodel

Basic SQLModel Setup

from sqlmodel import SQLModel, Field…

--

--

Towards AI
Towards AI

Published in Towards AI

We train AI Engineers. We deploy Enterprise AI. Join 100K+ AI practitioners at Towards AI Academy or reach out for help making your company AI native. towardsai.com