Configuring Python FastAPI with SqlAlchemy and Alembic

FastAPI

Starlette

Pydantic

ASGI specification

Uvicorn server

Alembic

Getting started

# Use python 3.6+
pipenv install fastapi uvicorn sqlalchemy alembic

Uvicorn installation error

ERROR: Cannot use 'python', Python 2.4 or later is required.", '           Note that Python 3 or later is not yet supported.'

Environment based configuration

pipenv install python-dotenv
.
├── Dockerfile
├── Pipfile
├── Pipfile.lock
├── README.md
├── alembic.ini
├── app
│ ├── __init__.py
│ ├── config
│ │ ├── __init__.py
│ │ ├── prod.env
│ │ └── qa.env
│ ├── database.py
│ ├── main.py
│ ├── models
│ │ ├── __init__.py
│ └── schemas
├── migration
│ ├── README
│ ├── env.py
│ ├── script.py.mako
│ └── versions
│ └── 66d8a54cd297_initial_tables.py
├── start.sh
#__init__.py
from pydantic import BaseSettings
class Settings(BaseSettings):
# default conf goes here
app_name: str = "Awesome API"
admin_email: str
items_per_user: int = 50
class Config:
env_file = ".env"

Alembic

# Initialize alembic database migration flow
$ alembic init db-migration
  • Update alembic’s env.py with environment based db_url with custom get_url method as shown below
  • update run_migration_offline as shown below
  • update run_migration_online as shown below
  • update target_metadata This is needed for autogeneration of migration scripts
env.py

Alembic autogeneration of migration scripts

  • For autogeneration to work, alembic should be able to detect all models that we have in the application.
  • Below __init__.py will help to automatically add all models that exists into __all__ so that we can do import * as shown in env.py
# This will scan the models and generate upgrade & downgrade scripts
alembic revision --autogenerate -m “Initial tables”

Running db migration

# following command will run all migration script and bring it to latest version
alembic upgrade head
# If we like to incrementally upgrade and check for some errors
alembic upgrade +1
# To undo last migration
alembic downgrade -1
# To get more information
alembic current
alembic history - verbose

Summary

  • SQLAlchemy Model creation is well documented in fastapi documentation. So this article is not diving into those details.
  • Configuring alembic with fastapi and getting autogeneration working is little tricky.
  • Above setup with env based config should come handy, if you are trying to setup one.

Useful links

--

--

--

Software Architect ★ Data engineer ★ Committed to improve data science productivity

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Coding a Website from Scratch

502 Bad Gateway | Nginx | Ubuntu | Magento 2

iOS interview preparation guide [part-1]

Modules in Python

How I passed my C|EH Practical and how you can too ?

KEYFI is going bananas!

Dulces memorias

Connecting MySQL server from remote/local

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sairam Krish

Sairam Krish

Software Architect ★ Data engineer ★ Committed to improve data science productivity

More from Medium

Python Flask ElasticSearch — indexer python command

An introduction to asyncio in python

Docker meets Python & Flask

Build High-Performance, Parallel, and Distributed Apps in Python