Created
March 5, 2015 16:50
-
-
Save arbinish/4adaf590cf869f3c158e to your computer and use it in GitHub Desktop.
db.session.dirty trick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask.ext.sqlalchemy import SQLAlchemy | |
from flask import Flask | |
app = Flask(__name__) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://db_user@localhost/employees' | |
db = SQLAlchemy(app) | |
class User(db.Model): | |
__tablename__ = 'myTable' | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String) | |
age = db.Column(db.Integer) | |
enabled = db.Column(db.Integer) | |
def update_(): | |
for user in User.query: | |
if user.age > 40: | |
user.enabled = 1 | |
if db.session.dirty: | |
try: | |
db.session.commit() | |
except Exception: | |
db.session.rollback() | |
if __name__ == '__main__': | |
update_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment