Search code examples
ioscore-datansfetchrequest

To-Many relationship (CoreData) with counters


I have a project with two entities - Ingredients and Dishes, with many to many relationships. In every dish I keep a relationship to the ingredients. I am trying to have a counter for the right quantity for each ingredient for every dish (for example, dish "Omlet" should have "Egg" with property "2" and "Milk" with property "1"....). I am struggling with the right way to model the counter and how to define it, any help there?


Solution

  • You need a third entity to model this. First, remove your to-many relationship. Then create a new entity called IngredientQuantity (or whatever you like) and add a property amount of type integer. Now, add two to-one relationships to IngredientQuantity. The first one points to your Dish entity and the second one to your Ingredient entity.