Skip to content

__fields_set__ incorrectly set unless using the validate class method #87

@decherd

Description

@decherd

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from sqlmodel import SQLModel
from typing import Optional
class HeroUpdate(SQLModel):
    name: Optional[str] = None
    secret_name: Optional[str] = None
    age: Optional[int] = None
hero = HeroUpdate(age=42)
print(hero.dict(exclude_unset=True))
# output = {'name': None, 'secret_name': None, 'age': 42}

Description

*Create a HeroUpdate model
*Create a HeroUpdate instance with unset fields
*Use the .dict attribute with the unset_fields parameter
*unset_fields show up as None rather than not being included in the dictionary
*works fine with Pydantic BaseModel instances

Operating System

macOS

Operating System Details

No response

SQLModel Version

'0.0.4'

Python Version

3.9.0

Additional Context

If it is run as:
hero = HeroUpdate.validate({'age':42})
the _fields_set_ are correctly set and it works

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions