开发者

Core Data Lookup Table

How can I model a mutable lookup table using Core Data?

Say I have a table called "Pai开发者_运维问答ntings". Each painting can have one or several "Colors", such as "red", "blue", etc. The "Colors" table is therefore shared.

How can I model this?

I want to be able to modify the "Colors" table as well by adding or deleting colors as well.


If you want to be able to traverse from a Color to all the Painting objects that use that color, then you would want to use to-many Painting-->>Color with to-many inverse relationship Color-->>Painting. This seems likely to be what you are looking for.

If, on the other hand, you do not need to traverse from Color to Painting, then this may be one of those rare cases where you would want to forego an inverse relationship and simply have to-many Painting-->>Color.

edit

If you want to be able to delete Colors that are references by Paintings, then you probably want to keep inverse relationships between these two entities. Furthermore, you will want to define 'delete rules' on the Color-->>Painting and on the Painting-->>Color relationships. In both cases, "nullify" seems the best choice. So, when a color is deleted, all paintings that reference it will simply have that reference nullified (removed). Likewise, when a painting is deleted, all colors that referenced it will cease to reference it.

In other words, there should be no need to check for lookup failures. That said, it is always good practice to code defensively.


Yes, a many-to-many relation is the correct way to model this.


You need a data model something like this:

Painting{
    name:string
    colors<<-->>Color.paintings
}

Color{
    name:string
    paintins<<-->>Painting.color
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜