0

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 ;)

  • You probably want to say that you actually want it to be a relationship? `delivery_address: QuoteDeliveryAddress = Relationship()` – MatsLindh Mar 27 '23 at 10:07
  • I think its more like a property of QuoteModel. And the QuoteDeliveryAddress isent stored in a separate table, its only a single instance witch is saved in the Quote table with the QuoteModel. Or is that a bad idea and i have to store each QuoteDeliveryAddress as a Model in a table? Like a Post? So i get very much data rows only for that construct. – Spooner Mar 27 '23 at 12:19
  • 1
    If you want to make `QuoteDeliveryAddress` part of `QuoteModel`, you'll have to inherit from it - not create a separate model. A separate model usually means a separate table, which is then linked in through a relationship. If you want to embed the fields inside `QuoteModel`, inherit from that class as well. As it's defined now, you've added a model inside another model, without telling SQLAlchemy anything about how that should work at all. – MatsLindh Mar 27 '23 at 12:39
  • Thanks for your answer. For this case its an good option simply inherit from the QuoteDeliveryAddressModel. For the next case i have an ItemsModel which is not an concrete Model because its configurable in some versions. The list of Items i cant inherit and i cant create a logical structure i think. A friend give me an input for saving the Quote into an Json-Database instead in a SQL-Database. So i can stor more complicated Models with much different typs of configurations. What do you think about that? – Spooner Mar 27 '23 at 16:34
  • Welcome to SO. I would recommend reading [this](https://stackoverflow.com/help/how-to-ask) and [this](https://stackoverflow.com/help/mcve) and then posting a separate question that follows those guidelines. – Daniil Fajnberg Mar 27 '23 at 20:26

0 Answers0