update table record

792 views
Skip to first unread message

Mason

unread,
Feb 3, 2012, 1:04:37 PM2/3/12
to sqlalchemy
Hi,

If i have a table named 'message' and it has the following columns:
message_id, tar_del, src_del. I pass in del_list which is a list of
message to delete. I tried the following

self.session.query(Message)\
.filter(Message.message_id.in_(del_list))\
.update({Message.tar_del: self._DELETE})

but kept getting this error

File "/usr/lib64/python2.7/site-packages/sqlalchemy/orm/query.py",
line 2598, in update
"Could not evaluate current criteria in Python. "
InvalidRequestError: Could not evaluate current criteria in Python.
Specify 'fetch' or False for the synchronize_session parameter.

What does the error mean? What I ended up doing is

rows = self.session.query(Message)\
.filter(Message.message_id.in_(del_list))\
.all()

for r in rows:
if key == 'src_del'
r.src_del = self._DELETE
elif key == 'tar_del'
r.tar_del = self._DELETE

and just loop through the rows return, but I think this is not very
efficient.

Also, to reference a table column, I do r.src_del and r.tar_del, is it
possible to make it a variable, so I can do something like r[key] =
self._DELETE?

Thanks,
Mason

Michael Bayer

unread,
Feb 3, 2012, 9:47:13 PM2/3/12
to sqlal...@googlegroups.com

On Feb 3, 2012, at 1:04 PM, Mason wrote:

> Hi,
>
> If i have a table named 'message' and it has the following columns:
> message_id, tar_del, src_del. I pass in del_list which is a list of
> message to delete. I tried the following
>
> self.session.query(Message)\
> .filter(Message.message_id.in_(del_list))\
> .update({Message.tar_del: self._DELETE})
>
> but kept getting this error
>
> File "/usr/lib64/python2.7/site-packages/sqlalchemy/orm/query.py",
> line 2598, in update
> "Could not evaluate current criteria in Python. "
> InvalidRequestError: Could not evaluate current criteria in Python.
> Specify 'fetch' or False for the synchronize_session parameter.

the error message refers to the "synchronize_session" parameter of the update method:

http://docs.sqlalchemy.org/en/latest/orm/query.html?highlight=query.update#sqlalchemy.orm.query.Query.update

pass "False" for this value instead of the default of "evaluate":

query.update({Message.tar_del:self._DELETE}, synchronize_session=False)


George Xie

unread,
Nov 29, 2012, 1:47:28 AM11/29/12
to sqlal...@googlegroups.com
synchronize_session='evaluate'
so, how this option really work?
Reply all
Reply to author
Forward
0 new messages