Skip to content

Optional ID value for auto-increment causes type errors when retrieving from database #365

@davecook88

Description

@davecook88

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

class MyModel(DBModel, table=True):

    id: Optional[int] = Field( primary_key=True)

Description

Thanks for the great library. I'm just

If I have a model like this:

class MyModel(DBModel, table=True):

    id: Optional[int] = Field( primary_key=True)

Then when saving new records to the database, the ID is automatically assigned, which is great.

However, when I retrieve the model like this I get type errors

model = session.get(MyModel, 1)
id: int = model.id  # ID may be None error

Is there a way to auto-assign my IDs but also have the ID type defined when retrieving saved records?

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.6

Python Version

3.10.4

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions