Skip to content

alembic database migration

create migration folder

  • The first time the project runs, the migrations folder is generated
alembic init -t async migrations

Modify the configuration file

  • env ​​file path: backend/migrations/env.py
# import SQLModel

from sqlmodel import SQLModel  

# import model data

from app.models import *  

# set metadata

target_metadata = SQLModel.metadata
  • ini file path: backend/alembic.ini
# Modify the asynchronous database connection of the project
sqlalchemy.url = sqlite+aiosqlite:///amisadmin.db

Generate migration files

  • Execute the command to generate the sqlModel initialization migration file
alembic revision --autogenerate

update database

- Execute command to update database: alembic_version
alembic upgrade head

Migration

  • The following commands are executed once every time the model is modified.
- Execute command to generate sqlModel update migration file
alembic revision --autogenerate

- Execute command to update database: alembic_version
alembic upgrade head

Reference documentation: