How sqlmodel works with FastApi in responses #1634
-
First Check
Commit to Help
Example Codeclass CardTagLink(SQLModel, table=True): card_id: int | None = Field(default=None, foreign_key="card.id", primary_key=True) tag_id: int | None = Field(default=None, foreign_key="tag.id", primary_key=True) class CardProjectLink(SQLModel, table=True): card_id: int | None = Field(default=None, foreign_key="card.id", primary_key=True) project_id: int | None = Field(default=None, foreign_key="project.id", primary_key=True) class Card(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str text: str tags: list["Tag"] = Relationship(back_populates="cards", link_model=CardTagLink, sa_relationship_kwargs={"lazy": "selectin"}) projects: list["Project"] = Relationship(back_populates="cards", link_model=CardProjectLink, sa_relationship_kwargs={"lazy": "selectin"}) class Tag(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str cards: list["Card"] = Relationship(back_populates="tags", link_model=CardTagLink) class Project(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str cards: list["Card"] = Relationship(back_populates="projects", link_model=CardProjectLink) # my repo class Repository: model: SQLModel def __init__(self, session): self.session = session def get_one(self, id_: int) -> 'SQLModel': statement = select(self.model).where(self.model.id == id_) return self.session.exec(statement).one() # my schemas class BaseResponseSchema(BaseModel): status: str = "OK" status_code: int = HTTP_200_OK message: str | None = None result: Any = None class CardBaseSchema(BaseResponseSchema): result: Card # my endpoint @router.get("/cards/{card_id}") async def read_card(card_id: int, db_client: DBClient = Depends(get_db_client)) -> CardBaseSchema: return CardBaseSchema( result=db_client.card.get_one(id_=card_id) )DescriptionWith my app i got result: My card has tags and projects but i don't see them in response. Operating SystemLinux Operating System DetailsUbuntu 24.04.2 LTS SQLModel Version0.0.27 Python VersionPython 3.12.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
| Have you tried following the docs? Create BTW, looking at the output you provided, it looks like you were using |
Beta Was this translation helpful? Give feedback.
-
| Thx, @YuriiMotov i missed it https://sqlmodel.tiangolo.com/tutorial/fastapi/multiple-models/#the-herocreate-data-model |
Beta Was this translation helpful? Give feedback.
Have you tried following the docs?
The first thing I noticed is that you are using "table" model (with
table=True) as a part of response model. You shouldn't use table-models for validation.Create
CardBasemodel with common fields (excluding relationships), then createCard(CardBase, table=True)and add relationships to it, then createCardOutput(CardBase)and duplicatetags: list["Tag"]andprojects: list["Project"](withoutRelationship). UseCardOutputinCardBaseSchema.BTW, looking at the output you provided, it looks like you were using
BaseResponseSchemaas response model that time.