Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from sqlmodel import Field, SQLModel, create_engine
Comment thread
YuriiMotov marked this conversation as resolved.
Outdated


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)
Comment thread
YuriiMotov marked this conversation as resolved.
Outdated

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
},
)