pip install aerich
from tortoise import Model, fields
class User(Model):
""" 用户基础信息 """
name = fields.CharField(max_length=24, description="姓名")
id_no = fields.CharField(max_length=24, description="身份证号")
- 创建配置db.py文件,配置TORTOISE_ORM
TORTOISE_ORM = {
"connections": {"default": "mysql://root:password@localhost/basename"},
"apps": {
"models": {
# models对应上面创建的models.py
"models": ["aerich.models", "models"],
"default_connection": "default",
},
},
}
- 生成初始化数据配置, db.TORTOISE_ORM是上面配置TORTOISE_ORM的路径
aerich init -t db.TORTOISE_ORM
- 生成后会生成一个
aerich.ini
文件和一个migrations
文件夹
- 初始化数据库
aerich init-db
aerich migrate
# 在后面加 --name=xxx, 可以指定文件名
aerich upgrade
aerich downgrade
官方文档:https://github.com/tortoise/aerich