Mohamed Abd El Raouf
Mohamed Abd El Raouf

Reputation: 928

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?

Upvotes: 12

Views: 25072

Answers (2)

YB Yu
YB Yu

Reputation: 372

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

This would work. Read about syncrhonize_session in sqlalchemy documentation.

Upvotes: 6

SingleNegationElimination
SingleNegationElimination

Reputation: 156158

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()

Upvotes: 22

Related Questions