dont have an example handy, but yeah youd want to make a
MapperExtension and work into the after_insert(), after_update() and
after_delete() hooks (or maybe the before_ versions of each one,
depending on how you detect changes).  you can issue writes to the
database immediately within those and theyll be within the current
transaction.

theres an example of a "write" side MapperExtension in examples/pickle/
custom_pickler.py .

if you want some help on actually detecting "whats changed", you can
use some features of the attributes package to do so.  if you have an
instance of MyClass and want to inspect the "history" of
instance.someattribute:

history = MyClass.someattribute.get_history(instance, passive=True)

"passive=True" means "dont fire off lazy loaders".  that call returns
to you an AttributeHistory object, with which you can say:

history.is_modified()
history.added_items() -> returns a list of new values, a one-element
list for scalar attributes
history.deleted_items() -> returns a list of deleted values, a one -
element list for scalar attributes
history.unchanged_items() -> etc

note that by "history", we mean things that have occured since the
instance was loaded from the database into the current session.

On Mar 28, 5:39 pm, "Arnar Birgisson" <[EMAIL PROTECTED]> wrote:
> Hi there,
>
> I have an old system that I'm porting over to SA. In the old system
> there is one entity that keeps a change history of itself. In the code
> that performs db updates, the current state of the object is examined
> and before it is updated I insert rows to a table with colums like
> this:
>
> object id
> change-event-id
> fieldname
> old-value
> new-value
>
> This is used to render a change history of the entity to the user.
>
> Now, can I automate this somehow with SA? Preferably I'd like to do
> this automatically on object update. Can I use the "dirty" set in the
> session to find out what columns of the mapped table have changed?
>
> How do I hook into the update action - I think I need a mapper
> extension, but can you point me to some examples?
>
> Has anyone done something similar (the whole changelog thing) with SA?
>
> Arnar


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to