Hi Folks,


I have an application that needs to do a certain amount of housekeeping when
objects are deleted or edited. Essentially, I need to keep historical data, so
under some circumstances "deleted" objects actually need to be "obsoleted", and
"edited" objects need to be copied and references to them from elsewhere
updated. Now, object modification can be caught and handled in the object
itself without (too much) hassle. But object deletion through a session is
another matter...

My first impulse was to use a mapper extension and before_delete. This is not
very satisfying, though - since before_delete is called "inside" a flush(), the
ORM can't be used, and manipulating the database straight through the
connection object during a flush() just seems brittle.
Another option is to put a .delete() method on the mapped object itself, and
mandate that objects should be deleted through this method. This means that
these objects would need to be treated differently througout the codebase,
which is not nice. That said, this is the approach I'm using at the moment.

What would be nice is if a method on an object could be called when
session.delete(object) is called, before the flush() happens. To do this,
Session.delete might be extended to provide MapperExtension-like functionality:
def delete(self, object, entity_name=None): ... if hasattr(object, "on_delete"):
           r = object.on_delete(self)
           if r == EXT_PASS:
               return
       ...

The same thing could be done for other session.X operations on mapped objects.
I'm not yet very familiar with SQLAlchemy's internals, so I'm sure there are
subtleties I haven't thought of, but you get the drift.

So, firstly: is there a nicer way to do this? Secondly, is a patch that does
something like the above a good idea?




Cheers,



Aldo


--
Aldo Cortesi
[EMAIL PROTECTED]
http://www.nullcube.com
Mob: 0419 492 863

--~--~---------~--~----~------------~-------~--~----~
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