Skip to content

Conversation

@busykoala
Copy link

Close #444

Due to the recursive loading problem loading relations is not yet possible. This change introduces the config option 'include_relations' to also load specifically chosen relations.

For the current use case nothing changes unless the user specifically sets fields to be included (and carefully considers the risks of circular includes). The option is very valuable if the table design contains many 1:n relations.

Additionally, if the new config option was used with fastapi the tests show that the relation additionally has to be set to sa_relationship_kwargs={"lazy": "selectin"}), while the relations will not be returned otherwise.

@codecov

This comment was marked as outdated.

@github-actions
Copy link
Contributor

📝 Docs preview for commit 96d8163 at: https://631dd936753845213d1e2387--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 84f63cf at: https://631dda323fbe07200d52088f--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 8a5ffcc at: https://631ddb6ebb7dd71b7b02620a--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit f6dc339 at: https://631ddc07663389246842205f--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 962c9c8 at: https://631ddeff5fa59526a8ff8fe5--sqlmodel.netlify.app

@kennedy
Copy link

kennedy commented Sep 26, 2022

bump

@github-actions
Copy link
Contributor

📝 Docs preview for commit 51aa03f at: https://639ce02ba12b8e02ba9de2b6--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit b408fa9 at: https://f3eafa08.sqlmodel.pages.dev

@tiangolo tiangolo added the feature New feature or request label Oct 22, 2023
Due to the recursive loading problem loading relations is not yet possible.
This change introduces the config option 'include_relations' to also load specifically chosen relations.

For the current use case nothing changes unless the user specifically sets fields to be included (and carefully considers the risks of circular includes).
The option is very valuable if the table design contains many 1:n relations.
@github-actions
Copy link
Contributor

📝 Docs preview for commit cbc9a0d at: https://2ea088ba.sqlmodel.pages.dev

@emp-l
Copy link

emp-l commented Jan 19, 2024

That'd be very helpful and will reduce duplicated code. Any plans to merge this PR any time soon? 🙏

@Galtozzy
Copy link

Galtozzy commented Mar 17, 2025

Bump, is very simple yet will be insanely useful

@busykoala
Copy link
Author

I haven’t looked into the latest changes on main, and I’m not confident about the current state of the code, since I haven’t worked on it in a while. I’m not sure what kind of impact this change would have on the codebase now, as things might have shifted quite a bit. Happy to revisit or close this if it’s more helpful for the project.

@github-actions
Copy link
Contributor

Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@busykoala, thanks for working on this!

For now there is a problem in this implementation - FastAPI fails to generate openapi schema (see in the details).

from typing import List, Optional

from fastapi import FastAPI
from sqlmodel import Field, Relationship, SQLModel

class Team(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    heroes: List["Hero"] = Relationship(  # noqa: F821
        back_populates="team", sa_relationship_kwargs={"lazy": "selectin"}
    )

    class Config:
        orm_mode = True
        include_relations = {"heroes"}

class Hero(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    powers: List["Power"] = Relationship(  # noqa: F821
        back_populates="hero", sa_relationship_kwargs={"lazy": "selectin"}
    )
    team_id: Optional[int] = Field(default=None, foreign_key="team.id")
    team: Optional[Team] = Relationship(back_populates="heroes")

    class Config:
        orm_mode = True
        include_relations = {"powers"}

class Power(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    description: str
    hero_id: Optional[int] = Field(default=None, foreign_key="hero.id")
    hero: Optional[Hero] = Relationship(back_populates="powers")

app = FastAPI()

@app.get("/")
async def read_main(response_model=List[Team]):
    pass
.venv/lib/python3.11/site-packages/fastapi/_compat.py", line 471, in get_schema_from_model_field
    return field_schema(  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pydantic/schema.py", line 248, in pydantic.schema.field_schema
  File "pydantic/schema.py", line 216, in pydantic.schema.get_field_info_schema
  File "pydantic/schema.py", line 995, in pydantic.schema.encode_default
  File "pydantic/json.py", line 90, in pydantic.json.pydantic_encoder
TypeError: Object of type '_GenericAlias' is not JSON serializable

Also, current implementation only works with Pydantic V1.

Do you have any ideas how to fix that?

@svlandeg svlandeg assigned svlandeg and unassigned svlandeg Sep 17, 2025
@github-actions
Copy link
Contributor

As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR.

@github-actions github-actions bot closed this Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request investigate waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Selectin for the SQLModels relations

7 participants