librelist archives

« back to archive

Flask-SQLAlchemy .commit() not updating database

Flask-SQLAlchemy .commit() not updating database

From:
Andrew Kaiser
Date:
2012-06-27 @ 15:47
I've just started using Flask along with the Flask-SQLAlchemy extension.
 Things have been fine for the most part but for some reason I don't seem
to be able to get updating a record to work properly.

I fetch the object using the query method, make model changes, then call
commit() (like I've seen in numerous examples).  However, upon checking the
database the changes are not made.

Here is a snippet of what I'm attempting:

@app.route('/admin/user/<int:id>', methods=['GET', 'POST'])
@login_required
def admin_user(id):
#user = User.query.filter(User.id == id).first()
user = User.query.get(id)
 if request.method == 'POST':
# get user/update
user.name = request.form['name']
 user.email = request.form['email']
if request.form['password'] != '':
 user.set_password(request.form['password'])

#User.query.filter(User.id == id).update({"name":"Andrew New"}) <- this
will update when I tested but is commented out
 db.session.commit()
flash('User edited')
return redirect(url_for('admin_user', id=id))
 return render_template('user.edit.html', user=user)

I'm using MySQL on the backend and have set the SQLALCHEMY_ECHO = True to
see the queries being executed.

When I run the code with the commented out line uncommented I see an update
statement, however, running the .commit() shows me no update statements and
this is confirmed by the fact that the database is not updated.

Any help in this matter would be greatly appreciated!



-- 
Andrew Kaiser | Principal | Angstrom Systems, Inc. | 612.239.3623

Re: [flask] Flask-SQLAlchemy .commit() not updating database

From:
ol
Date:
2012-06-27 @ 16:00
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi.

You are missing:
...
> db.session.add(user)
db.session.commit()
...

You are committing an empty session now, so no changes are made.

- -- ol


On 06/27/2012 05:47 PM, Andrew Kaiser wrote:
> I've just started using Flask along with the Flask-SQLAlchemy
> extension. Things have been fine for the most part but for some
> reason I don't seem to be able to get updating a record to work
> properly.
> 
> I fetch the object using the query method, make model changes, then
> call commit() (like I've seen in numerous examples).  However, upon
> checking the database the changes are not made.
> 
> Here is a snippet of what I'm attempting:
> 
> @app.route('/admin/user/<int:id>', methods=['GET', 'POST']) 
> @login_required def admin_user(id): #user =
> User.query.filter(User.id == id).first() user = User.query.get(id) 
> if request.method == 'POST': # get user/update user.name =
> request.form['name'] user.email = request.form['email'] if
> request.form['password'] != '': 
> user.set_password(request.form['password'])
> 
> #User.query.filter(User.id == id).update({"name":"Andrew New"}) <-
> this will update when I tested but is commented out 
> db.session.commit() flash('User edited') return
> redirect(url_for('admin_user', id=id)) return
> render_template('user.edit.html', user=user)
> 
> I'm using MySQL on the backend and have set the SQLALCHEMY_ECHO =
> True to see the queries being executed.
> 
> When I run the code with the commented out line uncommented I see
> an update statement, however, running the .commit() shows me no
> update statements and this is confirmed by the fact that the
> database is not updated.
> 
> Any help in this matter would be greatly appreciated!
> 
> 
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJP6y4FAAoJENnD1Z3aOEsz0gsIAKpJtR4ia8Syh0hZ2u97OEB7
W5Bzt37j2hxa3jwF7dMvrq6EDcwOZeFDkCHmoiwg9++OZdv7Atl7v5GbRCKDmKYC
0w4TCtQjstq3Ii/nOwu/v7qYFi1bvqJXjfORa9I9dh6/PI6s1gWgpYz5VI2dBaj7
hGGZkBARkyq1F5aqVGfQOd323pYxc/sawfEr+U9xzOBNr8eJRBIagMzDxo2JnsfW
pmOHQE+0hR4MCjhkG0XsOzLLAj8NzTfiQ7XJ8shL559t42AIPB3nkjSOT30HDFWx
2pIWsfCNMCtOKW0gcNE2uSSYQvvKFsBUQL+WfQoMi+QF17wzcLhnl5jzs8N8Miw=
=cGG9
-----END PGP SIGNATURE-----

Re: [flask] Flask-SQLAlchemy .commit() not updating database

From:
Andrew Kaiser
Date:
2012-06-27 @ 16:13
Thanks for the reply.  But this is an update of an object fetched using the
session.

If I do what you suggest I get the following error:

"InvalidRequestError: Object '<User at 0x101b82c90>' is already attached to
session '1' (this is '2')"

Which I've become well accustomed to.


If you read documentation that Armin has posted here:

http://lucumr.pocoo.org/2011/7/19/sqlachemy-and-you/

Specifically:
Updating Entries

How do you update an entry? Just get the object, modify it and commit the
session:

obj = MyModel.query.get(the_id)obj.name = 'New Value'session.commit()

And on Stackoverflow here:

http://stackoverflow.com/questions/9667138/how-to-update-sqlalchemy-row-entry

The method I'm using SHOULD work but does not.

Re: [flask] Flask-SQLAlchemy .commit() not updating database

From:
Andrew Kaiser
Date:
2012-06-27 @ 16:38
Not 100% sure why, but if I add

db.session.merge(obj)

before the call to commit() it works.

According to every example of Flask-SQLalchemy I've seen this shouldn't be
the case.

Re: [flask] Flask-SQLAlchemy .commit() not updating database

From:
ol
Date:
2012-06-27 @ 16:35
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sorry about that, was a bit quick.

Add
import pdb;pdb.set_trace()
just before the session.commit and check that your user.email and
user.name have been set according to your form input, and try
committing from there.
If you have ipython and ipdb installed use:
import ipdb;ipdb.set_trace()
for autocompletion ++.

(After set.trace your browser will hang and you get a python prompt in
your runserver terminal)

A bit too new with flask and sqlalchemy to come with some more head on
suggestions, but should help you debug further.


On 06/27/2012 06:13 PM, Andrew Kaiser wrote:
> Thanks for the reply.  But this is an update of an object fetched
> using the session.
> 
> If I do what you suggest I get the following error:
> 
> "InvalidRequestError: Object '<User at 0x101b82c90>' is already
> attached to session '1' (this is '2')"
> 
> Which I've become well accustomed to.
> 
> 
> If you read documentation that Armin has posted here:
> 
> http://lucumr.pocoo.org/2011/7/19/sqlachemy-and-you/
> 
> Specifically: Updating Entries
> 
> How do you update an entry? Just get the object, modify it and
> commit the session:
> 
> obj = MyModel.query.get(the_id)obj.name = 'New
> Value'session.commit()
> 
> And on Stackoverflow here:
> 
> http://stackoverflow.com/questions/9667138/how-to-update-sqlalchemy-row-entry
>
>  The method I'm using SHOULD work but does not.
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJP6zZRAAoJENnD1Z3aOEsztOkH/ikGqs9CP4rLhkA6cfWbFNmz
suCFGICoYrFztEuDJCXhDE/hntF3Iz95uQdrlR6UKIImDkrTFTvtQclHK6ZmKsjF
1xs+BlQInEMGzslyfxzu51xwOwcFAkOj0DWYnKsWtxHJ1kArRvs5kw1y4s1yOniO
Hp4z/zCLqxg+D5ssDODM8lGCY4x5uX62SCU2PHQ7c+Ah/bWk/40jcQ7Ox7vwMHZK
ClC+p4ZySzaa8bKiGWSkrRNhyoKIAh+6TEC7gg3uyRkdcq+55x/xRFDCBzPMFLVN
G1Y25T7rFk32D7WEm6/8zrUjxZMHkJtaHnP4ToDMdiE9WeFRc9d9bTrcWxpQ/BQ=
=otJ5
-----END PGP SIGNATURE-----