-
-
Notifications
You must be signed in to change notification settings - Fork 785
Add config option to load relationship fields. #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
|
📝 Docs preview for commit 96d8163 at: https://631dd936753845213d1e2387--sqlmodel.netlify.app |
|
📝 Docs preview for commit 84f63cf at: https://631dda323fbe07200d52088f--sqlmodel.netlify.app |
|
📝 Docs preview for commit 8a5ffcc at: https://631ddb6ebb7dd71b7b02620a--sqlmodel.netlify.app |
|
📝 Docs preview for commit f6dc339 at: https://631ddc07663389246842205f--sqlmodel.netlify.app |
|
📝 Docs preview for commit 962c9c8 at: https://631ddeff5fa59526a8ff8fe5--sqlmodel.netlify.app |
|
bump |
|
📝 Docs preview for commit 51aa03f at: https://639ce02ba12b8e02ba9de2b6--sqlmodel.netlify.app |
|
📝 Docs preview for commit b408fa9 at: https://f3eafa08.sqlmodel.pages.dev |
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.
|
📝 Docs preview for commit cbc9a0d at: https://2ea088ba.sqlmodel.pages.dev |
|
That'd be very helpful and will reduce duplicated code. Any plans to merge this PR any time soon? 🙏 |
|
Bump, is very simple yet will be insanely useful |
|
I haven’t looked into the latest changes on |
|
📝 Docs preview for commit a2f7711 at: https://630c5f7b.sqlmodel.pages.dev Modified Pages |
YuriiMotov
left a comment
There was a problem hiding this 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?
|
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. |
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.