我是 fastapi 和 SQLModel 的新手,我试图从我现有的库中实现一些基本代码,我有一个地址类
喜欢
@dataclass
class Address(DataClassJsonMixin):
addr1: str
city: str
province: str
我只是想在 SQLModel 中创建一个连接到 DB 的类。我在这里只添加了一个新的列 ID。我遇到错误,我不确定为什么它要求配置属性。
class AddressMaster(SQLModel, Address):
id: int = Field(default=None, primary_key=True)
AttributeError: type object 'Address' has no attribute '__config__'
它失败了config = getattr(base, "__config__")
,有一些我无法理解的信息。
# Only one of the base classes (or the current one) should be a table model
# this allows FastAPI cloning a SQLModel for the response_model without
# trying to create a new SQLAlchemy, for a new table, with the same name, that
# triggers an error
尝试1:
from sqlmodel import SQLModel, Field
from ...core import Address
from dataclasses import dataclass
@dataclass
class AddressDB(Address, SQLModel):
pass
# END AddressDB
class AddressMaster(AddressDB, table=True):
"""
Address Master Table
"""
id: int = Field(default=None, primary_key=True)
# END AddressMaster
对象创建
objAd = AddressMaster.from_dict({"addr1": "Kashmir", "city": "Srinagar", "province": "Kashmir"})