SqlAlchemy update does not work with Sqlite

I followed (two) examples in this question: SQLAlchemy: best way to upgrade with declarative?

And I found that the model update does not happen when using sqlite with flask-sqlalchemy on Ubuntu Linux. The simplest example doesn't work for me:

class Task: id= db.Column(db.Integer, primary_key=True) name= db.Column(db.String(32), unique=True) desc= db.Column(db.String(255), unique=False) state= db.Column(db.Boolean) # ... @app.route("/task/<int:id>/update",methods=["POST"]) def toggle_state(id): db.session.query(Task).get(id).update({"state":True}) log.info("state is now: " + str(Task.query.get(id).state)) # prints "state is now: False" 

For the first time, using the / sqlalchemy flask, so I guess I'm missing something obvious.

+8
source share
1 answer

So, I tried a few different things. Here's what didn't work out, and what ended up happening:

Does not work:

 # this will throw an exception, "AttributeError: 'Task' object has no attribute 'update'" db.session.query(Task).get(id).update({"state":state}) db.session.commit() # this was the example in the linked SO thread: # does nothing db.session.query(Task).filter_by(id=id).update({"state":state}) #this also does nothing task = Task.query.filter_by(id=id) task.state = state db.session.commit() #not this either task = Task.query.get(id) task.state = state db.session.commit() 

Was the work:

 #ok this works: db.session.query(Task).filter_by(id=id).update({"state":state}) db.session.commit() #and also this: task = db.session.query(Task).get(id) task.state = state db.session.commit() 
+20
source

All Articles