如何利用SQLModel获取模型来识别下面的模型?
from sqlmodel import Field, SQLModel
class Hero(SQLModel, table=True):
id: int = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None我一直在研究的一种方法是导入Alembic的SQLalchemy模型,但是通过查看源代码,我无法找到如何做到这一点。
如何使Alembic与SQLModel模型一起工作?
发布于 2021-09-05 13:30:06
在高级用户指南中应该有关于这方面的信息,并且比我的解释更好,但是这里是我如何使禁食迁移工作的。
首先,在控制台中运行alembic init migrations以生成迁移文件夹。在迁移中,文件夹应该是空的版本子文件夹、env.py文件、文件。在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 将您的更改应用于DB。
发布于 2022-02-27 05:01:11
在这里,您可以找到与异步SQLmodel数据库https://github.com/jonra1993/fastapi-sqlmodel-alembic的快速a alembic和https://github.com/jonra1993/fastapi-sqlmodel-alembic集成。
https://stackoverflow.com/questions/68932099
复制相似问题