#πŸ”’ FastAPI + SQLModel: Nested relationships not showing in API response

5 messages Β· Page 1 of 1 (latest)

arctic condor
#

I'm building an API with FastAPI and SQLModel, but I'm having trouble getting nested relationships to show up in my API responses. I've set up eager loading with selectinload, but the nested data isn't appearing in the output.

Here's my main Contact model:

class Contact(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    user_id: str = Field(foreign_key="user.id")
    user: User = Relationship(back_populates="contacts")
    fn: str
    nickname: Optional[str] = None
    org: Optional[str] = None
    title: Optional[str] = None
    birthday: Optional[int] = None
    note: Optional[str] = None
    name: Name = Relationship(back_populates="contact")
    emails: List[Email] = Relationship(back_populates="contact")
    phones: List[Phone] = Relationship(back_populates="contact")
    addresses: List[Address] = Relationship(back_populates="contact")
    socials: List[Social] = Relationship(back_populates="contact")
    websites: List[Website] = Relationship(back_populates="contact")

    class Config:
        populate_by_name = True

And here's my FastAPI route

@router.get("/", response_model=List[Contact])
def get_contacts(token: Token, session: Session = Depends(get_session)):
    query = (
        select(Contact)
        .options(
            selectinload(Contact.name),
            selectinload(Contact.emails),
            selectinload(Contact.phones),
            selectinload(Contact.addresses),
            selectinload(Contact.socials),
            selectinload(Contact.websites),
        )
        .join(User)
        .where(User.id == token.sub)
        .order_by(Contact.id)
    )
    contacts = session.exec(query).all()
    if not contacts:
        raise HTTPException(status_code=404, detail="No contacts found for this user")
    return contacts

However, the API response doesn't include any of the nested relationships (name, emails, phones, etc.)

keen ventureBOT
#

@arctic condor

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

arctic condor
#

When I print contacts[0].model_dump(), it doesn't show me the nested relationships, but when I print contacts[0].phones.model_dump() it shows me the phone numbers.

Ran the api with vscode debugger and saw that the nested relationships are present in the contacts pydantic model but just not showing in the api response.

Any ideas on what I might be missing or how to debug this further? Thanks in advance for any help!

keen ventureBOT
#

@arctic condor

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.