Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 20 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
@@ -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
},
)