Replies: 2 comments
-
I'm interested in this as well. As far as I can tell, the SQLModel docs showcase performing updates with the following: statement = select(Hero).where(Hero.name == "Spider-Boy")
results = session.exec(statement)
hero = results.one()
hero.age = 16
session.add(hero)
session.commit() However, this causes two SQL statements to fires (a SELECT followed by an UPDATE), whereas the command @OndrejIT has shared only fires one UPDATE. Unfortunately, the type errors mean we're not benefiting much from SQLModel, here. When I tested this myself I found my type errors were due to Avoiding the redundant SELECT is an important performance consideration so I'd love to learn if there's a well supported way of doing this! |
Beta Was this translation helpful? Give feedback.
-
Related to this, I'd like to be able to do things like As far as I know if you do this like Edit take 2: Here's my workaround that requires two type ignores to appease mypy (understandably!) statement = (
update(database.Account)
.where(database.Account.id == account_id)
.values(balance=database.Account.balance + amount)
)
self.session.exec(statement) Maybe integers could expose a add/subtract method in sqlmodel? Not sure if that would be the cleanest option or not. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
How to use update()?
self.session.execute or self.session.exec?
I would like to use the sqlmodel version of exec everywhere in the code, however I am running into a problem in typehint where the update method is not allowed to be accepted, so IDE throws me an error in that case, even though the code seems functional and the sql code the same in both cases.
Thank you.
Operating System
Linux
Operating System Details
docker.io/python:3.12-alpine3.19
SQLModel Version
0.0.14
Python Version
python:3.12
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions