There should be a __delete__ method that ORM classes can define

Issue #1065 resolved
Former user created an issue

Since an object can continue to exist in Python memory indefinitely after session.delete(...) has been called to remove it from the database, it would be fun if objects could define __delete__() and if SQLAlchemy would call that routine when an object was removed from the database. This could be the beginning of trigger-like functionality in SQLAlchemy.

A difficulty is that an object can be deleted from the database with session.delete(...), and then be added back with session.save(...) later. I can't imagine a use case for that, but it looks entirely possible. Therefore there might need to be a symmetrical __insert__() method that can tell the object it's live and stored in a database again so that it can do the reverse of whatever cleanup the object wanted done when the user removed it.

Comments (1)

  1. Mike Bayer repo owner

    this is supplied by MapperExtension.after_delete() and is straightforward to hook up to an on-class __delete__() method.

    There's been discussion about providing a prefab sqla extension module which supplies all the MapperExtension persistence methods on a class (or maybe as decorators) but end-user versions of this are trivial.

  2. Log in to comment