0

my question is about way to get a select to parent model, with properly selectinload option, using pydantic response model.

For example:

from pydantic import BaseModel

class Parent(SQLModel, table=True):
    id: UUID = sm.Field(UUID, primary_key=True)
    childs:List[Child]= sm.Relationship(
        back_populates="parent"
    )

class Child(SQLModel, table=True):
    parent_id:UUID=sm.Field()
        sa_column=sm.Column(
            sm.ForeignKey("parentr.id")
    )
    parent: "Parent" = sm.Relationship(
        back_populates="childs"
    )

#read schemas

class IChildRead(BaseModel):
    id:UUID

class IParentReadWithChilds(BaseModel):
    childs:List[IChildRead]

#result
select(Parent).options(selectinload(Parent.Childs))

The existance of property in pydantic model, tell us about necessity to use selectinload, of course the model can be very deep. I will be glad for any information.

I tried sqlmodel library, read documentation, but dont have answer for it.

  • The input is pydantic response model, the output is select with selectinload options like, sm.select(Parent).option(selectinload(Parent.property1).selectinload(Child_of_property1.property_of_child), selectinload(Parent.property2)) and so on. The recursive response model contain meta data , that this properties should be load not lazy. – Артур Лагунов Jan 26 '23 at 11:36

0 Answers0