Menu
Newbedev LogoNEWBEDEV Python Javascript Linux Cheat sheet
Newbedev LogoNEWBEDEV
  • Python 1
  • Javascript
  • Linux
  • Cheat sheet
  • Contact

Flask-SQLAlchemy with_for_update() row lock

You just need to state what you want to lock:

user = User.query.with_for_update(of=User).filter_by(id=userid).first()
user.money -= 0.1

After struggling for one whole day, i found the problem.

user = User.query.with_for_update().filter_by(id=userid).first()

should be

result = db.session.query(User.money).with_for_update().filter_by(id=userid).first()
money = result[0]
user.money = money - 0.1

Yes, so simple but annoying

Tags:

Python

Mysql

Locking

Flask

Flask Sqlalchemy

Related

How can you "clone" a conda environment into the root environment? How to hide the OK and Cancel buttons of antd Modal? How to format a complex table for rmarkdown PDF output Subscribe to both route params and queryParams in Angular 2 How make htaccess for single-page app within a sub-path in URL? How does sorting a string in an array of strings and then sorting that array come out to be O(a*s(loga+logs))? S3 last-modified timestamp for eventually-consistent overwrite PUTs What is a good practice to check if an environmental variable exists or not? Howto include clojure.spec'd functions in a test suite Django DRY Model/Form/Serializer Validation GraphQL mutation that accepts an array of dynamic size and common scalar types in one request Horizontal line in the middle of divs

Recent Posts

Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu 20.04 Build super fast web scraper with Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
© 2021 newbedevPrivacy Policy