Fynn
Fynn

Reputation: 4883

Change SQLAlchemy's Session.delete() behaviour

Are there any configuration possibilities concerning the delete()-method of SQLAlchemy's Sessions? I'd like to have the corresponding objects marked with a deleted flag in the database and not removed from it. Is there a way to achieve this? The aim is to build a database without destructive updates without losing the advantages of SQLAlchemy's cascading features.

Upvotes: 5

Views: 1660

Answers (1)

van
van

Reputation: 77012

Create your own session class inheriting from Session and override the delete() method with your own logic (for those classes that require logical delete), falling back to the default implemetation for the other objects. If you use sessionmaker or similar factory, you can provide your class in the class_ parameter as well.

Hopefully, this answers your question. But, having said/written that, there is SOOO MUCH MORE to the logical deletion especially in term of Referential Integrity, that one can write a series of articles on that.

Upvotes: 4

Related Questions