Pydantic orm_mode when relationship in relationship

Viewed 535

I'm trying to make my Pydantic model from my sqlachemy model, but I have some trouble cause I have a relationship in a relationship. Here's my code

The Pydantic model

class GradeSchemaOut(BaseSchema):
    student: StudentOutSchema
    score: float

    class Config:
        orm_mode = True

The grade class

from model.model_base import BaseModel


class Grade(BaseModel):
    result_id = Column(UUIDType, ForeignKey("result.id"), nullable=False)
    score = Column(Float)

    result = relationship("Result", back_populates="grades")

And the Result Class

from model.model_base import BaseModel


class Result(BaseModel):
    student_id = Column(UUIDType, ForeignKey("student.id"), nullable=False)

    student = relationship("Student", back_populates="results")
    grades = relationship("Grade", back_populates="result", cascade="all, delete-orphan")

So as you can see, in my GradeSchemaOut, i have the student attribute. But the student is within the Result which is within the Grade. So naturally, when i do GradeSchemaOut.from_orm(grade_model) it fails cause it doesn't know how to get the student. Anyone knows how can I fix it?

Thanks

0 Answers
Related