.sqlmodel_update() raise RuntimeError: dictionary changed size during iteration #982
-
First Check
Commit to Help
Example Codedef foo(hero_in:HeroCreate, related_id:int, owner_id:int):
hero.sqlmodel_update(
hero_in,
update={
"related_id": related_id,
"owner_id": owner_id,
},
)
#####
def sqlmodel_update(
self: _TSQLModel,
obj: Union[Dict[str, Any], BaseModel],
*,
update: Union[Dict[str, Any], None] = None,
) -> _TSQLModel:
use_update = (update or {}).copy()
if isinstance(obj, dict):
for key, value in {**obj, **use_update}.items():
if key in get_model_fields(self):
setattr(self, key, value)
elif isinstance(obj, BaseModel):
for key in get_model_fields(obj):
if key in use_update:
value = use_update.pop(key)
else:
value = getattr(obj, key)
setattr(self, key, value)
for remaining_key in use_update:
if remaining_key in get_model_fields(self):
value = use_update.pop(remaining_key) # NOTE the problem is here.
setattr(self, remaining_key, value)
else:
raise ValueError(
"Can't use sqlmodel_update() with something that "
f"is not a dict or SQLModel or Pydantic model: {obj}"
)
return self DescriptionHi @tiangolo , thanks for sqlmodel. for remaining_key in use_update:
if remaining_key in get_model_fields(self):
value = use_update.pop(remaining_key) # NOTE the problem is here.
setattr(self, remaining_key, value) Instead of Operating SystemmacOS Operating System DetailsMac Os M2 SQLModel Version0.0.16 Python Version3.10.14 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
BartSchuurmans
Jul 1, 2024
Replies: 1 comment
-
I've submitted a fix in #997 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
YuriiMotov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've submitted a fix in #997