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