Skip to content

b-bokma/recipe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQLAlchemy ORM 1.4 with FastAPI on asyncio

This is a tutorial app build for my blog post

Run project

docker-compose up

Run tests

docker-compose run app pytest

Database

The database is managed via sqlalchemy and alembic. The Base Model is set up in tables/base_class and imported all underlying models. Base.py imports all models to feed it to alembic

In config.settings you pass localhost (or 127.0.0.1) as db_host. In the .env file it is the name of the DB so docker will define the hostname.

update app/db/migrations/env.py

import Base models, import configs to load all metadata to alembic

from app.core.config import settings  # noqa
from app.db.base import Base  # noqa

config = context.config
config.set_main_option("sqlalchemy.url", settings.DATABASE_URL)
target_metadata = Base.metadata

fileConfig(config.config_file_name)`

initialize Alembic

alembic init app/db/migrations/

initiate a migration

  • run alembic stamp to move the migration scripts to the latest version. If the DB is behind, run alembic history to find the ID of the last migration file.
    • docker-compose run app alembic revision --autogenerate
  • When the file is created execute the migration
    • docker-compose run app alembic upgrade head

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published