You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question 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
deffoo(hero_in:HeroCreate, related_id:int, owner_id:int):
hero.sqlmodel_update(
hero_in,
update={
"related_id": related_id,
"owner_id": owner_id,
},
)
#####defsqlmodel_update(
self: _TSQLModel,
obj: Union[Dict[str, Any], BaseModel],
*,
update: Union[Dict[str, Any], None] =None,
) ->_TSQLModel:
use_update= (updateor {}).copy()
ifisinstance(obj, dict):
forkey, valuein {**obj, **use_update}.items():
ifkeyinget_model_fields(self):
setattr(self, key, value)
elifisinstance(obj, BaseModel):
forkeyinget_model_fields(obj):
ifkeyinuse_update:
value=use_update.pop(key)
else:
value=getattr(obj, key)
setattr(self, key, value)
forremaining_keyinuse_update:
ifremaining_keyinget_model_fields(self):
value=use_update.pop(remaining_key) # NOTE the problem is here.setattr(self, remaining_key, value)
else:
raiseValueError(
"Can't use sqlmodel_update() with something that "f"is not a dict or SQLModel or Pydantic model: {obj}"
)
returnself
Description
Hi @tiangolo , thanks for sqlmodel.
Related to PR #804
When I tried to update my existing db object which is hero and its related_id, owner_id fields I encountered RuntimeError: dictionary changed size during iteration. In the sqlmodel_update function The following part is manipulates the dictionary use_update. That's needed to be fixed I think.
forremaining_keyinuse_update:
ifremaining_keyinget_model_fields(self):
value=use_update.pop(remaining_key) # NOTE the problem is here.setattr(self, remaining_key, value)
Instead of value = use_update.pop(remaining_key) we can use update value = update.pop(remaining_key)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
Hi @tiangolo , thanks for sqlmodel.
Related to PR #804
When I tried to update my existing db object which is hero and its
related_id
,owner_id
fields I encountered RuntimeError: dictionary changed size during iteration. In the sqlmodel_update function The following part is manipulates the dictionary use_update. That's needed to be fixed I think.Instead of
value = use_update.pop(remaining_key)
we can use updatevalue = update.pop(remaining_key)
Operating System
macOS
Operating System Details
Mac Os M2
SQLModel Version
0.0.16
Python Version
3.10.14
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions