> -----Original Message-----
> From: sqlalchemy@googlegroups.com 
> [mailto:sqlalch...@googlegroups.com] On Behalf Of Stephen Hansen
> Sent: 25 August 2009 07:34
> To: sqlalchemy@googlegroups.com
> Subject: [sqlalchemy] Tracking Last Update timestamp for models
> 
> Hi all. 
> 
> I'm working on converting our system to use SQLAlchemy, and 
> one thing I'm not sure how to do properly is to track when a 
> particular model object is last updated. When doing just raw 
> SQL over the DB-API, this basically meant whenever I did a 
> change to the model, I would issue an UPDATE statement to 
> update the last update field as well.
> 
> When using model objects, I'm not sure how to best do that. 
> Is there a way to put a hook into the model objects that are 
> called whenever they're saved to the database, and add a new 
> change as well? Or do I have to make it so all fields are 
> synonyms() and properties that update this attribute at the 
> same time as setting the primary one?
> 
> Thanks in advance.
> 
> --S
> 

SQLAlchemy column definitions can include an "onupdate" parameter. From
the docs:

onupdate - A scalar, Python callable, or ClauseElement representing a
default value to be applied to the column within UPDATE statements,
which wil be invoked upon update if this column is not present in the
SET clause of the update. This is a shortcut to using ColumnDefault as a
positional argument with for_update=True.

So if you pass something like sqlalchemy.func.current_timestamp() as the
onupdate parameter to a Column, you should get the behaviour you want.

Hope that helps,

Simon

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@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