"ValueError: <class 'src.models.calls.Call'> has no matching SQLAlchemy type" when adding Relationship #908
-
First Check
Commit to Help
Example Codefrom sqlmodel import Field, Relationship, SQLModel
from datetime import datetime
from uuid import UUID, uuid4
class Call(SQLModel, table=True):
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
messages: list["Transcription"] = Relationship(back_populates="call")
### TRANSCRIPT ###
class BaseTranscription(SQLModel):
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
message: str
timestamp: datetime
is_remote: bool
is_final: bool
call_id: UUID = Field(foreign_key="call.id")
call: Call = Relationship(back_populates="messages")
class Transcription(BaseTranscription, table=True):
pass DescriptionHey, I have the following classes as shown in the example code. If I run this I get the following error: However, when I run this after removing "call: Call = Relationship(back_populates="messages")" from the BaseTranscription class it actually works. I'm folllowing the documentation (https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/back-populates/#fix-it-using-back_populates) and it should be the same so I don't see why this doesn't work. Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.16 Python Version3.12.2 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
class BaseTranscription(SQLModel):
call: Call # optional if you don't need it on your base class
class BaseTranscription(BaseTranscription, table=True):
call: Call = Relationship(back_populates="messages") # relationships should be defined on tables only |
Beta Was this translation helpful? Give feedback.
-
@joachimhuet I'm sorry I forgot the last part of the example code. Here it is: |
Beta Was this translation helpful? Give feedback.
Well then, as I stated in my answer you can move the relationship in the
Transcription
class and it will work. You can still keepcall: Call
in the base class but just without the= Relationship(...)
part. 😸