fastapi 使用 TORTOISE-ORM
pip install tortoise-orm
配置
TORTOISE_ORM = {
'connections': {
'default': {
# 'engine': 'tortoise.backends.asyncpg', PostgreSQL
'engine': 'tortoise.backends.mysql', # MySQL or Mariadb
'credentials': {
'host': '127.0.0.1',
'port': '3306',
'user': 'root',
'password': 'root',
'database': 'fastapi',
'minsize': 1,
'maxsize': 5,
'charset': 'utf8mb4',
"echo": True
}
},
},
'apps': {
'models': {
'models': ['models',"aerich.models"],
'default_connection': 'default',
}
},
'use_tz': False,
'timezone': 'Asia/Shanghai'
}
使用aerich 迁移数据
pip install aerich
pip install tomlkit
相关操作
aerich init -t settings.TORTOISE_ORM
aerich init-db
aerich migrate
aerich upgrate
aerich downgrade