高级用户指南中应该很快就会有关于此的信息,并且解释比我的更好,但这里是我如何使 Alimbic 迁移工作。
首先alembic init migrations
在您的控制台中运行以生成迁移文件夹。内部迁移文件夹应该是空版本子文件夹、env.py文件、script.py.mako文件。在script.py.mako文件中,我们应该import sqlmodel
在这两行周围添加一行
#script.py.mako
from alembic import op
import sqlalchemy as sa
import sqlmodel # added
然后我们应该编辑env.py文件
#env.py
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
from app.models import * # necessarily to import something from file where your models are stored
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = None
# comment line above and instead of that write
target_metadata = SQLModel.metadata
写作时想到了一个想法,你忘记从你的models.py(或你的模型存储的任何其他地方)导入一些东西。这是主要问题
此外,一个重要的注意事项是通过按ctrl(CMD) + S保存模型中的更改- 这有一些问题。
最后,运行
alembic revision --autogenerate -m "your message"
应该使用您的更改在版本文件夹中生成一个新的 .py 文件。和
alembic upgrade head
将您的更改应用到数据库。