[openstack-dev] [nova] odd behaviour from sqlalchemy

Roman Podolyaka rpodolyaka at mirantis.com
Sat Oct 12 13:28:20 UTC 2013


Hello Chris,

I thought it was a bug in SQLAlchemy code, so I wrote a snippet [1] to
check my assumption, but I haven't managed to reproduce the problem with
SQLAlchemy versions 0.7.9, 0.7.10 and 0.8.2.

I would suggest you to start from enabling logging of all SQL queries
SQLAlchemy issues [2] and, if needed, examining of session/model instance
state with pdb.

For your second question. You can set a column to its current value by
using of literal_column() expression [3].

Can you elaborate a bit more on your use case? Why do you update the table
row, but keep the update_at column value unchanged?

Thanks,
Roman

[1] http://paste.openstack.org/show/48335/
[2]
https://github.com/openstack/nova/blob/stable/grizzly/nova/openstack/common/db/sqlalchemy/session.py#L296
[3]
https://github.com/openstack/cinder/blob/master/cinder/db/sqlalchemy/api.py#L1088


On Sat, Oct 12, 2013 at 2:31 AM, Chris Friesen
<chris.friesen at windriver.com>wrote:

> Hi,
>
> I'm using grizzly with sqlalchemy 0.7.9.
>
> I'm seeing some funny behaviour related to the automatic update of
> "updated_at" column for the Service class in the sqlalchemy model.
>
> I added a new column to the Service class, and I want to be able to update
> that column without triggering the automatic update of the "updated_at"
> field.
>
> While trying to do this, I noticed the following behaviour.  If I do
>
>         values = {'updated_at': new_value}
>         self.service_update(context, service, values)
>
> this sets the "updated_at" column to new_value as expected.  However, if I
> do
>
>         values = {'updated_at': new_value, 'other_key': other_value}
>         self.service_update(context, service, values)
>
> then the other key is set as expected, but "updated_at" gets auto-updated
> to the current timestamp.
>
> The "onupdate" description in the sqlalchemy docs indicates that it "will
> be invoked upon update if this column is not present in the SET clause of
> the update".  Anyone know why it's being invoked even though I'm passing in
> an explicit value?
>
>
> On a slightly different note, does anyone have a good way to update a
> column in the Service class without triggering the "updated_at" field to be
> changed?  Is there a way to tell the database "set this column to this
> value, and set the updated_at column to its current value"?  I don't want
> to read the "updated_at" value and then write it back in another operation
> since that leads to a potential race with other entities accessing the
> database.
>
> Thanks,
> Chris
>
> ______________________________**_________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.**org <OpenStack-dev at lists.openstack.org>
> http://lists.openstack.org/**cgi-bin/mailman/listinfo/**openstack-dev<http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstack.org/pipermail/openstack-dev/attachments/20131012/3ca45d23/attachment.html>


More information about the OpenStack-dev mailing list