I'm on the way to create a fastAPI app and have some issues to become an simple SQLModel as a single instance into an other SQLModel. I think the way wasent wrong to do that, but still become an error, when im starting the Server (uvicorn) and create the tables (sqlite).
Here my Models im trying to create. The reason is that i only need an Address once into an Quote, because its only the delivery address and not the customer address where's referenced with the user_id.
class QuoteDeliveryAddress(SQLModel):
title: str = None
contact: str = None
street: str = None
number: str = None
zip: str = None
country: str = None
class QuoteModel(SQLModel):
created_at: datetime = Field(default_factory=datetime.utcnow)
last_change: datetime = Field(default_factory=datetime.utcnow)
object_name: str
delivery_address: QuoteDeliveryAddress
user_id: int = Field(foreign_key="users.id")
class Quote(QuoteModel, table=True):
__tablename__ = "quotes"
id: Optional[int] = Field(default=None, primary_key=True)
number: Optional[int] = Field(default=1000000, unique=True)
user: Optional[User] = Relationship(back_populates="quotes")
The error is like this:
ValueError: The field delivery_address has no matching SQLAlchemy type
But why? The QuoteDeliveryAddress-Model is based on SQLModel like other Models is have there.
What I also tried:
delivery_address: QuoteDeliveryAddress = None
delivery_address: QuoteDeliveryAddress = Field(default=None)
delivery_address: Optional[QuoteDeliveryAddress] = Field(default=None)
So I hope you can help me with this issue, because i have to do the same with an list of ItemsModel
where i get the same error, but step by step ;)