Search code examples
iosswiftcore-datamany-to-many

Add index to CoreData many to many relation


I have 2 entities in CoreData related as many to many. Word and Translation. I want Translations to have priority, which is specific for word. Example:

Words: "abc", "daf"
Translations: "x", "y", "z"
"x" is translation for "abc" with rating 1, "y" with rating 2
"y" is translation for "daf" with rating 1, "x" with rating 2, "z" with rating 3

Using SQL i would store that rating value in support table for many-to-many relationship. Where do i store it in CoreData?


Solution

  • You need to either make it a one-to-many relationship (ie have a different Translation entity for every Word) or have another intermediate object (Word <<->> possibleTranslationsWithRank <<-->> Translation).

    The first solution is easier and simpler, so unless you have a lot of data in the Translation object besides its name and rank then I would go with that.

    Just as a warning: when situations like this occur for me (the unexpected need for a new Entity) I take it as a warning sign that I may not have fully thought about the system in a way that truly models what I am trying to represent.