在多对多结构中删除 [英] Deletions in a many-to-many structure

查看:106
本文介绍了在多对多结构中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想检查真的很快。假设我在数据模型中有两个实体:目录和产品。它们彼此具有多对多关系,并且都是必需的(目录必须至少有一个产品,并且所有产品必须至少属于一个目录)。所以如果我要删除一个产品,它的删除应该是Nullify,当然。

I just want to check really quickly. Say I have two entities in a data model: Catalog, and Product. They have a many-to-many relationship with each other, and both are required (a Catalog must have at least one Product, and all Products must each belong to at least one Catalog). So if I was to delete a Product, its deletion should be Nullify, of course.

但是,删除政策应该是什么目录?如果目录被删除,并不是所有的产品都必须属于它。产品可能属于多个目录。所以我绝对不应该使用Cascade。但是,Nullify是否足够?如果我结束了不属于目录的悬挂产品怎么办? Core Data内置的是什么来解决这个问题与多对多模式?我需要修改我的模式吗?

But what should the deletion policy be for Catalog? If a Catalog is deleted, not all of its Products necessarily exclusively belong to it. A Product may belong to more than one Catalog. So I definitely shouldn't use Cascade. However, is Nullify sufficient? What if I end up with dangling Products that don't belong to a Catalog? What does Core Data have built in that would resolve this issue with many-to-many schemas? Do I need to modify my schema?

推荐答案

Nullify是足够的,多对多的声音是正确的。

Nullify is sufficient, and many-to-many sounds right. The specific constraint you want (deleting orphans) is not directly enforceable by core data, though, so you get to do a little cleanup yourself.

具体来说,实现<$ c。你需要的特定约束(删除孤儿)不能直接由核心数据强制执行,因此你可以自己做一点清理。 $ c> willSave 在你的实体类中,并让每个实体测试:我不会删除;和,我没有相关联(产品/目录)?如果是,请删除自己。 (未删除的测试对于避免 willSave 的无限循环很重要。)

Specifically, implement willSave in your entity classes, and have each entity test: am I not deleted; and, do I have no associated (products/catalogs)? If so, delete myself. (the not-deleted test is important to avoid an infinite loop of willSaves.)

这会推迟删除的孤立目录或产品,直到保存时间。这可能不是问题。

This postpones the deletion of the orphaned catalogs or products until save time. This is probably not a problem.

这篇关于在多对多结构中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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