关系的核心数据属性? [英] Core Data attribute on relationship?

查看:54
本文介绍了关系的核心数据属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Core Data。



我有一个头痛实体和一个 Medicine 实体。头痛和药物之间存在多对多的关系。



头痛时,可以选择多种药物。我希望能够指定这些药物的数量。我对MySQL更加熟悉,您将在其中创建一个数据透视表,并在数据透视表中包括头疼ID和药ID,以便每个实例都可以有一个数量。



是否可以在Core Data中创建这种关系?




缺点是头痛实体中不再存在药物关系,而 Medicine 实体。



如果您确实想维持两个实体的多对多关系,则可以添加一个剂量属性药物,但这会改变您的提取物的性质。即,您必须获取所有剂量的特定药物,才能找出该药物治疗了哪些头痛。



您应该考虑要执行的特定提取,以及您打算如何遍历对象图,然后将这些需求纳入模型的设计中。


I'm just getting started with Core Data.

I have a Headache entity and a Medication entity. There is a many to many relationship between Headaches and Medications.

When you add a headache, you can choose multiple medications. I want to be able to specify quantities of those medications. I am more familiar with MySQL, where you would create a pivot table and include the quantity in the pivot table along with the headache_id and the medication_id so each instance could have a quantity.

Is there some way to create this kind of relationship in Core Data?

This is my Xcode data model.

解决方案

A many-to-many relationship can be modeled in Core Data. Core Data automatically creates an intermediate join table for you in the (SQLLite) persistent store.

What you can't do is add any attributes to that intermediate join table, since it is not a part of the object graph data model.

To represent the quantity, you could add a Dose entity to the object graph to model a many-one-many relationship between headaches, a quantity, and medications. This would represent the pivot you're thinking of, and the dose would function in the same way as an intermediate join table that Core Data would have created.

The downside is that there's no longer a medications relationship in the Headache entity, or a headaches relationship in the Medication entity.

If you really want to maintain that two-entity many-to-many relationship, you could add a dose attribute to the medication, but it would change the nature of your fetches. I.e., you would have to fetch all doses of a particular medication to find out which headaches were treated by that medication.

You should consider the specific fetches you'd be performing, and how you plan to traverse the object graph, then factor those requirements into the design of the model.

这篇关于关系的核心数据属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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