You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optional ID value for auto-increment causes type errors when retrieving from database #365
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:
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)
ifmodelisNone:
raisesqlalchemy.exc.NoResultFoundmodel.id# no more type warning here
Activity
antont commentedon Jun 24, 2022
I don't think what you say happens - if you get the object like that, the id will be
1notNonephi-friday commentedon Jun 24, 2022
add line
assert model.id is not Nonebefore declare variable idid = model.idRobertRosca commentedon Jul 1, 2022
The issue here isn't that
idmay beNone, it's thatmodelmay beNone, assession.getwill returnNoneif no entry is found - if you're using VSCode you can see this by hovering overmodel:You should add a check to deal
modelbeingNonebefore trying to accessidand that will solve the problem:Hope that helps!