SQLAlchemy with_for_update


SQLAlchemy tip:

You can use with_for_update() to use SELECT FOR UPDATE. This will prevent changes in selected rows before you commit your work to the database.

https://docs.sqlalchemy.org/en/14/orm/query.html#sqlalchemy.orm.Query.with_for_update

👇

user = session.query(User).filter(User.email == email).with_for_update().first()

user.is_active = True
session.add(user)
session.commit()