Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix fields marked as "set" in models #117

Merged
merged 7 commits into from
Aug 27, 2022
Merged
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
2 changes: 1 addition & 1 deletion sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,9 @@ def __init__(__pydantic_self__, **data: Any) -> None:
# Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy
# can handle them
# object.__setattr__(__pydantic_self__, '__dict__', values)
object.__setattr__(__pydantic_self__, "__fields_set__", fields_set)
for key, value in values.items():
setattr(__pydantic_self__, key, value)
object.__setattr__(__pydantic_self__, "__fields_set__", fields_set)
non_pydantic_keys = data.keys() - values.keys()
for key in non_pydantic_keys:
if key in __pydantic_self__.__sqlmodel_relationships__:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_fields_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from datetime import datetime, timedelta

from sqlmodel import Field, SQLModel


def test_fields_set():
class User(SQLModel):
username: str
email: str = "test@test.com"
last_updated: datetime = Field(default_factory=datetime.now)

user = User(username="bob")
assert user.__fields_set__ == {"username"}
user = User(username="bob", email="bob@test.com")
assert user.__fields_set__ == {"username", "email"}
user = User(
username="bob",
email="bob@test.com",
last_updated=datetime.now() - timedelta(days=1),
)
assert user.__fields_set__ == {"username", "email", "last_updated"}