views:

416

answers:

1

I have a CoreData datamodel that includes a many-to-many relationship. As it turns out NSPredicate does not support many-to-many relationships. From CoreData.pdf: "You can only have one to-many element in a keypath in a predicate."

As a Recipe example: Many recipes and many ingredients. Many recipes have the ingredient "salt" and "salt" is used in many recipes. This is a natural many-to-many relationship.

What are suggested work-arounds?
Was CoreData a bad idea and I should go back to SQLite?

+2  A: 

My understanding is that you can make any many-to-many relationship into separate one-to-many relationships by adding an intermediate entity.

You have:
Recipe has many Ingredients.
Ingredient has many Recipes.

Create a new RecipeIngredient entity such that:
Recipe has many RecipeIngredients.
Ingredients has many RecipeIngredients.
A RecipeIngredient has one Recipe and one Ingredient.

gerry3
Yes, that is basically what I have decided to do. CoreData creates the intermediate table, just can't use it in a NSPredicate.
zaph
But if YOU create the intermediate entity, then you CAN use it with an NSPredicate. In general, it is better to create a model that meets your needs and let Core Data handle the details then to worry about how Core Data might be implementing your model in SQLite
gerry3