One to many relationship doesn't return the many #600
Answered
by
danielmarkow
danielmarkow
asked this question in
Questions
-
First Check
Commit to Help
Example Code# schema.py
import uuid as uuid_pkg
from typing import List
from sqlmodel import SQLModel, Field, Relationship
class GradeInput(SQLModel):
grade: int
snippet: str
theme_id: uuid_pkg.UUID
class GradeOutput(GradeInput):
id: uuid_pkg.UUID
class Grade(GradeInput, table=True):
id: uuid_pkg.UUID | None = Field(default_factory=uuid_pkg.uuid4, primary_key=True)
theme_id: uuid_pkg.UUID = Field(foreign_key="theme.id")
theme: "Theme" = Relationship(back_populates="grades")
class ThemeInput(SQLModel):
theme: str
differentiation: str
class Theme(ThemeInput, table=True):
id: uuid_pkg.UUID | None = Field(default_factory=uuid_pkg.uuid4, primary_key=True)
grades: List["Grade"] = Relationship(back_populates="theme")
class ThemeOutput(ThemeInput):
id: uuid_pkg.UUID
grades: List[GradeOutput] = []
# main.py
@app.get("/api/theme")
def get_theme(session: Session = Depends(get_session)) -> list:
query = select(Theme)
return session.exec(query).all() DescriptionI expected this model to return
Yet all it returns is
Any idea why the grades key does not appear? Operating SystemmacOS Operating System DetailsApple M1 MacOS Ventura 13.3.1 SQLModel Version0.0.8 Python Version3.11.3 Additional ContextI am using sqlite. |
Beta Was this translation helpful? Give feedback.
Answered by
danielmarkow
May 21, 2023
Replies: 1 comment
-
Solved it. The issues was not in the schema definition but in the return type of the route:
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
danielmarkow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solved it. The issues was not in the schema definition but in the return type of the route: