一、FastAPI(TortoiseORM+Aerich)
demo项目结构:(migrations和pyproject.toml是持久化后生成的文件)

pip install tortoise_orm
pip install aerich
1.初始化数aerich配置,确定访问的数据库源,以及包含的models等
(.venv) PS D:\code\socketdemo01> cd src
(.venv) PS D:\code\socketdemo01\src> aerich init -t testapi_quickstart.settings.TORTOISE_ORM
Success writing aerich config to pyproject.toml
Success creating migrations folder ./migrations
2.初始化数据库(数据库需要提前创建,表不需要)只需要执行一次
pip install aiomysql
(.venv) PS D:\code\socketdemo01\src> aerich init-db
Success creating app migration folder migrations\models
Success generating initial migration file for app "models"
Success writing schemas to database "test"

3.生成迁移脚本(随意修改一个model,添加一个字段)
from tortoise.models import Modelfrom tortoise import fieldsclass Book(Model):id = fields.IntField(pk=True)name = fields.CharField(max_length=32, description="名称")desc = fields.CharField(max_length=32, description="描述")price = fields.IntField(default=0, description="价格")counts = fields.IntField(default=0, description="数量")# 一对多关系attrs = fields.ForeignKeyField("models.Attr", related_name="books")# 多对多关系plats = fields.ManyToManyField("models.Plat", related_name="books", description="书籍平台表")class Attr(Model):id = fields.IntField(pk=True)name = fields.CharField(max_length=32, description="属性名称")types = fields.CharField(max_length=32, description="属性类型")class Plat(Model):id = fields.IntField(pk=True)name = fields.CharField(max_length=32, description="平台名称")
(.venv) PS D:\code\socketdemo01\src> aerich migrate --name tbl_plat_add_desc_field
Success creating migration file 1_20251130204932_tbl_plat_add_desc_field.py
4.应用迁移脚本(将3.中生成的迁移版本应用到数据库)
应用前:

应用后:
(.venv) PS D:\code\socketdemo01\src> aerich upgrade
Success upgrading to 1_20251130204932_tbl_plat_add_desc_field.py

5.其他操作
a.查看历史迁移版本
(.venv) PS D:\code\socketdemo01\src> aerich history
0_20251130204549_init.py
1_20251130204932_tbl_plat_add_desc_field.py
b.回退迁移版本
(.venv) PS D:\code\socketdemo01\src> aerich downgrade
Downgrade is dangerous: you might lose your data! Are you sure? [y/N]: y
Success downgrading to 1_20251130204932_tbl_plat_add_desc_field.py
c.查看还未应用的迁移版本(可被迁移的版本清单)
(.venv) PS D:\code\socketdemo01\src> aerich heads
1_20251130204932_tbl_plat_add_desc_field.py
6.多数据源支持
TORTOISE_ORM = {"connections": {"default": "mysql://root:123456@127.0.0.1:3306/test","second": "postgres://postgres:123@localhost:5432/test",},"apps": {"models": {"models": ["aerich.models", "shop.models"],"default_connection": "default",},"models_second": {"models": ["user.models"],"default_connection": "second",},}, }
备注:
aerich.models 生成migrate持久化的版本信息表(如果需要重新执行2.),确保不报错,可以将migrations文件夹删除,并将aerich表清空
flask也可以类似操作
二、Flask(flask-migrate+sqlalchemy)
demo项目结构:(同理:migrations也是持久化后生成的目录)

pip install flask
pip install flask-sqlalchemy
pip install flask-migrate
pip install pymysql
1.初始化migrate配置(只做准备,不会直接应用schema;区别于aerich init-db)
(.venv) PS D:\code\zlgFlaskDemo> flask db init
config_name:develop
Creating directory D:\code\zlgFlaskDemo\migrations ... done
Creating directory D:\code\zlgFlaskDemo\migrations\versions ... done
Generating D:\code\zlgFlaskDemo\migrations\alembic.ini ... done
Generating D:\code\zlgFlaskDemo\migrations\env.py ... done
Generating D:\code\zlgFlaskDemo\migrations\README ... done
Generating D:\code\zlgFlaskDemo\migrations\script.py.mako ... done
Please edit configuration/connection/logging settings in D:\code\zlgFlaskDemo\migrations\alembic.ini before proceeding.
2.创建迁移脚本
(.venv) PS D:\code\zlgFlaskDemo> flask db migrate -m "初始化数据库"
config_name:develop
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.autogenerate.compare] Detected added table 'abc'
INFO [alembic.autogenerate.compare] Detected added table 't_menu'
INFO [alembic.autogenerate.compare] Detected added table 't_role'
INFO [alembic.autogenerate.compare] Detected added table 'tb_category'
INFO [alembic.autogenerate.compare] Detected added table 't_role_menu'
INFO [alembic.autogenerate.compare] Detected added table 't_user'
INFO [alembic.autogenerate.compare] Detected added table 'tb_attribute'
Generating D:\code\zlgFlaskDemo\migrations\versions\43daf0e230e1_初始化数据库.py ... done
3.应用迁移
(.venv) PS D:\code\zlgFlaskDemo> flask db upgrade
config_name:develop
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> 43daf0e230e1, 初始化数据库

5.其他操作(和aerich类似,功能大差不差)
flask db stamp head 查看版本
flask db revision -m "Description of migration" 重新生成版本
flask db downgrade 回退版本
flask db heads 查询目前存在的数据库版本
flask db current 查询目前现在使用的数据库版本
flask db heads 查询的数据库版本
e38935822969 (head)
f55fde3d62b1 (head)
flask db merge e38935822969 f55fde3d62b1 合并两个版本
flask db stamp ecb671d1eb4b
三、综上
两个框架的持久化,操作非常相似,大差不差。
TortoiseORM+Aerich完美契合FaskAPI异步接口开发。