SQLAlchemy update neither works nor raises errors
<p>I want to update the <code>email</code> column of the <code>user</code> table where <code>id == 3</code>. Below code does not update any rows or even raise any errors. Where could the problem be?</p>

<pre><code>from sqlalchemy import update
@app.route('/testupdate/')
def testupdate():
    stmt = update(user).where(user.id==3).values(email='a@b')
    db.session.commit()
    return 'done'
</code></pre>

<p>The problem is that no SQL was emitted to the database.</p>

<pre><code>    stmt = update(user).where(user.id==3).values(email='a@b')
</code></pre>

<p>Does not actually execute the statement, it just creates it. To actually execute it you have to pass it to</p>

<pre><code>    db.session.execute(stmt)
</code></pre>

<p>and then commit. You could also instead just use the <a href="http://docs.sqlalchemy.org/en/latest/orm/query.html" rel="nofollow"><code>Query</code></a> API of the session:</p>

<pre><code>    db.session.query(user).\
        filter_by(id=3).\
        update({user.email: 'a@b'},
               synchronize_session='evaluate')
</code></pre>

<p>Note that depending on whether or not you have the affected instances in the session and will be using them later on you could choose not to synchronize at all with <code>False</code>.</p>

python sqlalchemy flask-sqlalchemy