I used the GitHub search to find a similar issue and didn't find it.
I searched the SQLModel documentation, with the integrated search.
I already searched in Google "How to X in SQLModel" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to SQLModel but to Pydantic.
I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
I commit to help with one of those options 👆
Example Code
fromtypingimportList, OptionalfromsqlmodelimportField, Relationship, Session, SQLModel, create_enginefrompydanticimportBaseModelclassTeam(SQLModel, table=True):
id: Optional[int] =Field(default=None, primary_key=True)
heroes: List["Hero"] =Relationship(back_populates="team")
classHero(SQLModel, table=True):
id: Optional[int] =Field(default=None, primary_key=True)
team_id: Optional[int] =Field(default=None, foreign_key="team.id")
team: Optional[Team] =Relationship(back_populates="heroes")
classHero2(BaseModel):
id: intteam: TeamclassTeam2(BaseModel):
id: intheroes: List["Hero2"]
# What I would like to be able to dod= {"id": 123, "team": {"id": 124}} # fails# d = {"id": 123, "team": Team(**{"id": 124})} # succeedsh=Hero(**d)
print(h)
# The same idea but using vanilla Pydantic modelsd2= {"id": 123, "team": {"id": 124}}
h2=Hero2(**d2)
print(h2)
Description
In Pydantic it is possible to instantiate objects directly from dicts (i.e. JSON) via ClassName(**dict). This also works for objects with nested objects (i.e. relationships in SQLModel). Is it possible to do the same in SQLModel? I would like to take a JSON like {"id": 123, "relationship_obj": {"id": 456, ...}} and have SQLModel correctly create the relationship model based on the type of the field & the key of the dict passed in.
The error received is:
File "/.../venv/lib/python3.9/site-packages/sqlalchemy/orm/attributes.py", lin
e 1729, in emit_backref_from_scalar_set_event
instance_state(child),
AttributeError: 'dict' object has no attribute '_sa_instance_state'
One thing I don't understand here is how FastAPI instantiates SQLModel objects from the JSON request object. I think there must be support for what I want to achieve, it's just not easily accessible or well documented.
Hm I thought it works, am using from_orm in our service too, but not sure if the relationships work in this - is discussed there, saying they would not work: #224
I just ran into this same issue. Drove me nuts trying to figure it out. I imagine many JSON posts to FastAPI have such nested statements so I'm surprised that this is an issue. I keep thinking I must be doing something wrong.
Activity
JLHasson commented on May 17, 2022
One thing I don't understand here is how FastAPI instantiates SQLModel objects from the JSON request object. I think there must be support for what I want to achieve, it's just not easily accessible or well documented.
antont commented on Jun 24, 2022
Hm I thought it works, am using from_orm in our service too, but not sure if the relationships work in this - is discussed there, saying they would not work: #224
I do have them in my return values.
hugolytics commented on Jul 8, 2022
I am noticing a similar issue, even doing Hero(team=Team(...)).team will give a Hero object has no attribute team..
Any way to fix this?
sychou commented on Jul 20, 2022
I just ran into this same issue. Drove me nuts trying to figure it out. I imagine many JSON posts to FastAPI have such nested statements so I'm surprised that this is an issue. I keep thinking I must be doing something wrong.
daniil-berg commented on Sep 7, 2022
I think this is essentially a duplicate of this:
I agree that this is worthwhile, but I suggest closing this duplicate issue.
ewbrowntech commented on Apr 21, 2025
Did you ever find a way around this issue?