update existing row

1,435 views
Skip to first unread message

vitsin

unread,
Jul 30, 2011, 3:51:42 PM7/30/11
to sqlalchemy
hi,
can't figure out why raw SQL works fine, but update() is not working:
1.working raw SQL:
self.session.execute("update public.my_table set
status='L',updated_at=now() where my_name='%s'" % (self.my_name))

2.non working update() from Alchemy:
s = aliased(MyTable)
query = self.session.query(s).filter(s.my_name==self.my_name)
sts = self.session.execute(query).fetchone()
sts.update(values={'status':'L'})

sts.update(values={s.status:'L'})
File "/usr/lib/python2.6/dist-packages/sqlalchemy/engine/base.py",
line 2097, in _key_fallback
"Could not locate column in row for column '%s'" % key)
sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for
column 'update'"


But Column s.status exists ...
appreciate any help,
--vs

vitsin

unread,
Jul 30, 2011, 7:58:10 PM7/30/11
to sqlalchemy
Maybe someone can just place an example of correct update() usage
please: can't sort it out from docs rather google, thanks.

King Simon-NFHD78

unread,
Aug 1, 2011, 5:51:46 AM8/1/11
to sqlal...@googlegroups.com

In your example, 'sts' represents a single row from the database. These
objects don't have an 'update' method, which is why you are getting that
error. It thinks you are trying to access a column called 'update'
instead.

You appear to be using the SQL Expression language (ie. MyTable is
created using sqlalchemy.Table). You can create an 'update' statement
using MyTable.update(). Examples are at:

http://www.sqlalchemy.org/docs/core/tutorial.html#inserts-and-updates

(You should be able to substitute conn.execute() with session.execute())

However, you might be interested in using the ORM part of SQLAlchemy:

http://www.sqlalchemy.org/docs/orm/tutorial.html

Your usage would then look something like this (assuming MyMappedClass
is the class mapped to MyTable):

s = MyMappedClass
query = self.session.query(s).filter(s.my_name == self.my_name)
sts = query.first()
sts.status = 'L'
self.session.flush()

Hope that helps,

Simon

vitsin

unread,
Aug 3, 2011, 2:41:12 PM8/3/11
to sqlalchemy
thanks a lot! helpful.

On Aug 1, 5:51 am, "King Simon-NFHD78"
Reply all
Reply to author
Forward
0 new messages