Skip to content

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
Member

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 fastapi: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.

2 participants