From 6cec19c8dcd867d940b773d6e3f6e2dca316cb0f Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Tue, 23 Dec 2025 17:23:54 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Fix=20`RuntimeError:=20dictionary?= =?utf8?q?=20changed=20size=20during=20iteration`=20in=20`sqlmodel=5Fupdat?= =?utf8?q?e()`=20(#997)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com> --- sqlmodel/main.py | 3 +-- tests/test_update.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/test_update.py diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 7c916f79..f168fc02 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -1004,9 +1004,8 @@ class SQLModel(BaseModel, metaclass=SQLModelMetaclass, registry=default_registry else: value = getattr(obj, key) 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): - value = use_update.pop(remaining_key) setattr(self, remaining_key, value) else: raise ValueError( diff --git a/tests/test_update.py b/tests/test_update.py new file mode 100644 index 00000000..de4bd6cd --- /dev/null +++ b/tests/test_update.py @@ -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 + }, + ) -- 2.47.3