Introduction to SQLAlchemy 2 In Practice
#Python

Introduction to SQLAlchemy 2 In Practice

Python Reporter
3 min read

Miguel Grinberg announces the release of his comprehensive SQLAlchemy 2 book, sharing the preface and planning to publish all eight chapters on his blog over the coming weeks.

I'm excited to share that I'm publishing my book "SQLAlchemy 2 In Practice" here on my blog, starting with the preface today. This book, which I originally wrote in 2023, offers an in-depth look at SQLAlchemy version 2 - the current version of the most popular database library and Object-Relational Mapper (ORM) for Python.

Featured image

Why This Book Matters

Back in 2012, I started the Flask Mega-Tutorial, which has helped countless developers take their first steps into web development. Interestingly, the second most-read chapter of that tutorial isn't about Flask basics - it's Chapter Four, which introduces databases and SQLAlchemy.

Over the years, I've received hundreds of questions from developers who need to do more complex database operations than what introductory tutorials cover. Many struggle to figure out how to solve these problems on their own. This book is my attempt to address that knowledge gap.

What You'll Build

Rather than creating a complete web application, this book focuses on building a flexible, efficient, real-world relational database. You'll work on database operations for a fictional vintage home computer store called RetroFun, including:

  • Maintaining product lists categorized by attributes like year of release, manufacturer, country of origin, or CPU
  • Tracking customers and their orders
  • Managing star ratings and reviews
  • Tracking page views for blog articles
  • Generating various reports with a focus on efficiency and performance

Book Structure

The book contains eight chapters that will be published here over the coming weeks:

  1. Preface (this article)
  2. Database Setup
  3. Database Tables
  4. One-To-Many Relationships
  5. Many-To-Many Relationships
  6. Advanced Many-To-Many Relationships
  7. A Page Analytics Solution
  8. Asynchronous SQLAlchemy
  9. SQLAlchemy and the Web

Prerequisites

This isn't a book for complete beginners. You should have some previous experience writing Python and ideally some basic relational database knowledge. If you've learned databases through my Flask Mega-Tutorial or any other introductory Python course, you're at the right level.

How to Follow Along

I encourage you to write and try all the code examples as you read. The code has been thoroughly tested using SQLAlchemy 2.0 on SQLite, MySQL, and PostgreSQL. You'll need:

  • A recent Python interpreter
  • A text editor or IDE you're comfortable with
  • Basic terminal or command prompt knowledge

All source code and data files are available on GitHub.

Conventions Used

The book uses standard conventions for terminal commands (shown with $ prompts) and Python REPL sessions (shown with >>> prompts). Multi-line SQLAlchemy queries are broken up for readability but can be typed as single lines.

Technical Support

The book was reviewed by Martin Bell, Rostislav Roznoshchik, and my son Dylan Grinberg. I'm also grateful to Mike Bayer (creator of SQLAlchemy) and Federico Caselli (current maintainer) for their assistance and review.

Get the Book

If you can't wait for the blog installments, you can buy the book in electronic or paper format today. Your support directly helps my work - and I'll be eternally thankful!

Stay tuned for Chapter 1: Database Setup, coming soon!

Comments

Loading comments...