更改SQLAlchemy的Session.delete()行为 [英] Change SQLAlchemy's Session.delete() behaviour

查看:606
本文介绍了更改SQLAlchemy的Session.delete()行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于SQLAlchemy的Sessionsdelete()方法是否存在任何配置可能性?我想在数据库中用删除标记标记相应的对象,而不是将其从数据库中删除.有没有办法做到这一点?目的是在不破坏性更新的情况下构建数据库,而又不会失去SQLAlchemy的级联功能的优势.

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.

推荐答案

创建一个继承自Session的会话类,并使用自己的逻辑覆盖delete()方法(对于那些需要logical delete的类),回到其他对象的默认实现.如果您使用 sessionmaker 或类似工厂也可以在class_参数中提供您的课程.

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.

希望这能回答您的问题.但是,在说了这一点之后,逻辑删除还有 SOOO MUCH MORE ,尤其是在Referential Integrity方面,人们可以为此写一系列文章.

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.

这篇关于更改SQLAlchemy的Session.delete()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆