Skip to content

Commit 6cec19c

Browse files
🐛 Fix RuntimeError: dictionary changed size during iteration in sqlmodel_update() (#997)
Co-authored-by: Motov Yurii <[email protected]>
1 parent fb331e7 commit 6cec19c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

sqlmodel/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,8 @@ def sqlmodel_update(
10041004
else:
10051005
value = getattr(obj, key)
10061006
setattr(self, key, value)
1007-
for remaining_key in use_update:
1007+
for remaining_key, value in use_update.items():
10081008
if remaining_key in get_model_fields(self):
1009-
value = use_update.pop(remaining_key)
10101009
setattr(self, remaining_key, value)
10111010
else:
10121011
raise ValueError(

tests/test_update.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from sqlmodel import Field, SQLModel
2+
3+
4+
def test_sqlmodel_update():
5+
class Organization(SQLModel, table=True):
6+
id: int = Field(default=None, primary_key=True)
7+
name: str
8+
headquarters: str
9+
10+
class OrganizationUpdate(SQLModel):
11+
name: str
12+
13+
org = Organization(name="Example Org", city="New York", headquarters="NYC HQ")
14+
org_in = OrganizationUpdate(name="Updated org")
15+
org.sqlmodel_update(
16+
org_in,
17+
update={
18+
"headquarters": "-", # This field is in Organization, but not in OrganizationUpdate
19+
},
20+
)

0 commit comments

Comments
 (0)