50% OFF

ESP32-IDF Workshop

Blog/AI Tools

Building a Production FastAPI App with SQLAlchemy

A practical guide to building a FastAPI application with SQLAlchemy models, Pydantic schemas, and route handlers — organized into tabs for easy navigation.

| Intermediate
Rajath Kumar
Rajath KumarEdge AI Engineer & Founder, Analog Data
·10 min read
Building a Production FastAPI App with SQLAlchemy

Why Tabbed Code Blocks?

When building a FastAPI app, you typically split your code across multiple files: models.py, schemas.py, main.py, and database.py. Showing all of them stacked vertically makes the article long and hard to navigate. Tabbed code blocks solve this — same language, different files, one compact component.

The Full Application

Here's a complete FastAPI app with SQLAlchemy, organized into tabs. Click through each tab to see the different files:

1from sqlalchemy import Column, Integer, String, DateTime, Boolean
2from sqlalchemy.sql import func
3from database import Base
4
5class User(Base):
6    __tablename__ = "users"
7
8    id = Column(Integer, primary_key=True, index=True)
9    email = Column(String, unique=True, index=True, nullable=False)
10    username = Column(String, unique=True, index=True, nullable=False)
11    hashed_password = Column(String, nullable=False)
12    is_active = Column(Boolean, default=True)
13    created_at = Column(DateTime(timezone=True), server_default=func.now())
14    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

How It Works

Each tab is a separate Python file, but they all work together:

  1. models.py — SQLAlchemy ORM models defining the database schema
  2. schemas.py — Pydantic models for request/response validation
  3. database.py — Engine and session configuration
  4. main.py — FastAPI routes that tie everything together

Video Walkthrough

Want to see it in action? Here's a great intro to FastAPI by its creator, Sebastián Ramírez:

What the Community Says

💡

FastAPI is one of the fastest-growing Python frameworks. Here's what developers are saying:

Loading tweet…
Loading LinkedIn post…

Example Gist

Here's a minimal FastAPI starter as a GitHub gist — fork it and start building:

Loading gist…

Running the App

bash
1# Install dependencies
2pip install fastapi sqlalchemy psycopg2-binary passlib[bcrypt] pydantic[email]
3
4# Run the server
5uvicorn main:app --reload --port 8000

That's it. The tabbed code block keeps all four files in one place without overwhelming the reader.

Share
Live Workshop

Go from Arduino to Production Firmware

The ESP32-IDF Workshop covers ESP-IDF from scratch — tasks, queues, OTA, Wifi management, and deploying firmware that doesn't break at 3am.

Join the Workshop →

Frequently Asked Questions

Quick answers to common questions

Rajath Kumar

Written by

Rajath Kumar

Edge AI Engineer & Founder, Analog Data

I build things that run on chips and the software that talks to them. ESP32, STM32, FreeRTOS, FastAPI, TinyML — from bare-metal firmware to cloud backends to on-device inference. Based in Bengaluru. Founder of Analog Data.

More in AI Tools