From bacd6d9aa7659a39a38ec04cb22bc39bfa1cfcf0 Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Mon, 1 Jul 2024 17:58:00 +0200 Subject: [PATCH 1/3] Fix "RuntimeError: dictionary changed size during iteration" in sqlmodel_update() --- sqlmodel/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 38c85915aa..b77246b2cc 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -1001,9 +1001,8 @@ def sqlmodel_update( 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( From 0ed7c7c6d03fbe82dcc2166b2914cb7acf5b1949 Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Thu, 21 Aug 2025 18:05:50 +0200 Subject: [PATCH 2/3] chore: add test --- tests/test_update.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/test_update.py diff --git a/tests/test_update.py b/tests/test_update.py new file mode 100644 index 0000000000..4f952a1688 --- /dev/null +++ b/tests/test_update.py @@ -0,0 +1,23 @@ +from sqlmodel import Field, SQLModel, create_engine + + +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 + + engine = create_engine("sqlite:///", echo=True) + SQLModel.metadata.create_all(engine) + + 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 + }, + ) From a104920f1ab08673a363efec985c05a5077348bd Mon Sep 17 00:00:00 2001 From: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com> Date: Thu, 21 Aug 2025 22:20:03 +0200 Subject: [PATCH 3/3] Simplify test --- tests/test_update.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_update.py b/tests/test_update.py index 4f952a1688..de4bd6cdd2 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -1,4 +1,4 @@ -from sqlmodel import Field, SQLModel, create_engine +from sqlmodel import Field, SQLModel def test_sqlmodel_update(): @@ -10,9 +10,6 @@ class Organization(SQLModel, table=True): class OrganizationUpdate(SQLModel): name: str - engine = create_engine("sqlite:///", echo=True) - SQLModel.metadata.create_all(engine) - org = Organization(name="Example Org", city="New York", headquarters="NYC HQ") org_in = OrganizationUpdate(name="Updated org") org.sqlmodel_update(