简体   繁体   English

在SqlAlchemy中,合并时如何忽略M2M关系属性?

[英]In SqlAlchemy, how to ignore m2m relationship attributes when merge?

There is a m2m relation in my models, User and Role. 在我的模型中,用户和角色之间存在m2m关系。

I want to merge a role, but i DO NOT want this merge has any effect on user and role relation-ship. 我想合并一个角色,但我不希望此合并对用户和角色关系有任何影响。 Unfortunately, for some complicate reason, role.users if not empty. 不幸的是,由于某些复杂的原因,如果不为空,则为role.users。

I tried to set role.users = None, but SA complains None is not a list. 我试图设置role.users = None,但是SA抱怨None不是列表。

At this moment, I use sqlalchemy.orm.attributes.del_attribute, but I don't know if it's provided for this purpose. 目前,我使用sqlalchemy.orm.attributes.del_attribute,但是我不知道是否为此目的提供了它。

You'd better fix your code to avoid setting role.users for the item you are going to merge. 您最好修复代码,以避免为要合并的项目设置role.users But there is another way - setting cascade='none' for this relation. 但是还有另一种方式-为此关系设置cascade='none' Then you lose an ability to save relationship from Role side, you'll have to save User with roles attribute set. 然后,您将失去从“ Role侧保存关系的功能,您必须保存“ User并设置“ roles属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM