Search code examples
ioscore-dataentityrelationshipternary

Ternary relationships in coreData


Lets say I have entities Article, Page and Category. I want entity Article to have a relationship like this. Article (A1) has a category (C1) for page (P1), but I want the same article (A1) to have a category (C2) for another page (P2).

How do I model this in coreData? I'm guessing maybe one of these relationships doesn't have to implement its inverse relationship, but I'm not sure about that. Any ideas?


Solution

  • Why not have the page entity keep track of it's category?

    You could then provide a method on article that loops through all the page's and returns an NSSet of category's. If this was to be used often you could consider caching the result

    Update

                          +----------+
                          | Category |              
                          +----------+
                                |
                                V
    +---------+         +--------------+         +------+
    | Article | <---->> | CategoryPage | <<----> | Page |
    +---------+         +--------------+         +------+
    
    • An article can have many pages through category_page
    • A page can belong to many articles through category_page

    I don't think you can simply do

    +---------+         +------+
    | Article | <<--->> | Page |
    +---------+         +------+
    

    because you also need the category information