Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating specific row in SQLAlchemy

I'm using SQLAlchemy with python and i want to update specific row in a table which equal this query:

UPDATE User SET name = 'user' WHERE id = '3'

I made this code by sql alchemy but it's not working:

session.query(User).filter(User.id==3).update({'name': 'user'})

returned this error:

InvalidRequestError: Could not evaluate current criteria in Python. Specify 'fetch' or False for the synchronize_session parameter.

How can i do it?

like image 529
Mohamed Abd El Raouf Avatar asked Mar 02 '14 22:03

Mohamed Abd El Raouf


2 Answers

ormically, you don't use update(), you set attributes:

a_user = session.query(User).filter(User.id == 3).one()
a_user.name = "user"
session.commit()
like image 158
SingleNegationElimination Avatar answered Oct 13 '22 19:10

SingleNegationElimination


session.query(User).filter(User.id==3).update({'name':'user'},synchronize_session=False)

This would work. Read about syncrhonize_session in sqlalchemy documentation.

like image 6
YB Yu Avatar answered Oct 13 '22 20:10

YB Yu