Skip to content

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

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

Activity

  1. antont commented on Jun 24, 2022

    @antont

    I don't think what you say happens - if you get the object like that, the id will be 1 not None

  2. phi-friday commented on Jun 24, 2022

    @phi-friday
    Contributor

    add line assert model.id is not None before declare variable id id = model.id

  3. RobertRosca commented on Jul 1, 2022

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

    The issue here isn't that id may be None, it's that model may be None, as session.get will return None if no entry is found - if you're using VSCode you can see this by hovering over model:

    image

    You should add a check to deal model being None before trying to access id and that will solve the problem:

    model = session.get(MyModel, 1)
    if model is None:
        raise sqlalchemy.exc.NoResultFound
    
    model.id  # no more type warning here

    Hope that helps!

  4. locked and limited conversation to collaborators on Aug 11, 2025
  5. converted this issue into a discussion #1474 on Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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