mirror of
https://github.com/fastapi/sqlmodel.git
synced 2026-03-13 09:29:54 +08:00
🐛 Fix RuntimeError: dictionary changed size during iteration in sqlmodel_update() (#997)
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
This commit is contained in:
@@ -1004,9 +1004,8 @@ class SQLModel(BaseModel, metaclass=SQLModelMetaclass, registry=default_registry
|
|||||||
else:
|
else:
|
||||||
value = getattr(obj, key)
|
value = getattr(obj, key)
|
||||||
setattr(self, key, value)
|
setattr(self, key, value)
|
||||||
for remaining_key in use_update:
|
for remaining_key, value in use_update.items():
|
||||||
if remaining_key in get_model_fields(self):
|
if remaining_key in get_model_fields(self):
|
||||||
value = use_update.pop(remaining_key)
|
|
||||||
setattr(self, remaining_key, value)
|
setattr(self, remaining_key, value)
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
|||||||
20
tests/test_update.py
Normal file
20
tests/test_update.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from sqlmodel import Field, SQLModel
|
||||||
|
|
||||||
|
|
||||||
|
def test_sqlmodel_update():
|
||||||
|
class Organization(SQLModel, table=True):
|
||||||
|
id: int = Field(default=None, primary_key=True)
|
||||||
|
name: str
|
||||||
|
headquarters: str
|
||||||
|
|
||||||
|
class OrganizationUpdate(SQLModel):
|
||||||
|
name: str
|
||||||
|
|
||||||
|
org = Organization(name="Example Org", city="New York", headquarters="NYC HQ")
|
||||||
|
org_in = OrganizationUpdate(name="Updated org")
|
||||||
|
org.sqlmodel_update(
|
||||||
|
org_in,
|
||||||
|
update={
|
||||||
|
"headquarters": "-", # This field is in Organization, but not in OrganizationUpdate
|
||||||
|
},
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user