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.
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:
models.py— SQLAlchemy ORM models defining the database schemaschemas.py— Pydantic models for request/response validationdatabase.py— Engine and session configurationmain.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:
Example Gist
Here's a minimal FastAPI starter as a GitHub gist — fork it and start building:
Running the App
1# Install dependencies
2pip install fastapi sqlalchemy psycopg2-binary passlib[bcrypt] pydantic[email]
3
4# Run the server
5uvicorn main:app --reload --port 8000That's it. The tabbed code block keeps all four files in one place without overwhelming the reader.
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.
Frequently Asked Questions
Quick answers to common questions

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.