> -----Original Message-----
> From: sqlalchemy@googlegroups.com 
> [mailto:sqlalch...@googlegroups.com] On Behalf Of Michael Hipp
> Sent: 19 August 2010 23:39
> To: sqlalchemy@googlegroups.com
> Subject: Re: [sqlalchemy] Which columns changing during orm commit?
> 
> On 8/19/2010 5:24 AM, Chris Withers wrote:
> > Michael Hipp wrote:
> >> SQLAlchemy seems pretty smart about updating only the 
> changed columns
> >> in an orm object...
> >>
> >> If I have an orm object. Something changes one of the columns. Just
> >> before I commit() the session, is there a way to tell which columns
> >> will be updated vs those that are unchanged?
> >>
> >> Any way to ascertain the before/after values on those 
> changed columns?
> >
> > Here's the basics:
> >
> > http://www.sqlalchemy.org/docs/session.html#session-attributes
> >
> > These examples should fill in the rest:
> >
> > http://www.sqlalchemy.org/trac/wiki/UsageRecipes/VersionedMap
> > http://www.sqlalchemy.org/trac/wiki/UsageRecipes/VersionedRows
> > http://www.sqlalchemy.org/docs/examples.html?#module-versioning
> 
> Thanks. But I believe all those items deal with which orm 
> objects (rows) are 
> changed. I'm asking about columns within an orm object that 
> might be changed.
> 
> Did I miss something?
> 
> Thanks,
> Michael
> 

You could use mapper.iterate_properties [1] to loop over all the
properties of your object, and for each one call attributes.get_history
[2] to find out if it has changed. I'm not sure if it's the best way,
but it should work.

The return value from get_history isn't documented, but the source is
pretty simple. If you only want to know if the attribute has changed,
you can call the 'has_changes' method. You can look at the 'added' and
'deleted' properties to get the before and after values.

Hope that helps,

Simon


[1]
http://www.sqlalchemy.org/docs/reference/orm/mapping.html#sqlalchemy.orm
.mapper.Mapper.iterate_properties

[2]
http://www.sqlalchemy.org/docs/reference/orm/mapping.html#attribute-util
ities

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to