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

Conversation

statt8900
Copy link
Contributor

This request addresses the problem brought up in issue #87
By using the setattr to set even the default values of the SQLModel they all get added to the pydantic.BaseModel.__fields_set__ attribute regardless if the user sets them. Simply, moving the setattr for the __fields_set__ to after this iteration correctly recreates the intended behavior from pydantic. I've included the small test below that passes on my machine along with the other unit tests.

from datetime import datetime, timedelta
from sqlmodel import SQLModel, Field


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"}

@tiangolo tiangolo changed the title Fix fields set 🐛 Fix fields marked as "set" in models Aug 27, 2022
@tiangolo
Copy link
Owner

Awesome, thanks for writing thorough tests for this! 🍰 ☕

This fix will be available in SQLModel 0.0.7, released in the next hours. 🎉

@tiangolo tiangolo enabled auto-merge (squash) August 27, 2022 22:58
@tiangolo tiangolo merged commit 680602b into tiangolo:main Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants