Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data predicate : unimplemented SQL generation for predicate

Basically I got 3 entities in my data model : Brand, Model and Trim.

  • A brand has a one-to-many relationship with Model called "models". (one brand have multiple models but a model has only one brand)
  • A model has a many-to-many relationship with Trim called "trims". (a model can have multiple trims, and a trim can have multiple models)

Having an array of trims objects, I would like to get all the brands having a model which "contains" at least one trim contained inside this array.

So here is my predicate for the fetch request :

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Brand"];    
[NSPredicate predicateWithFormat:@"ANY models.trims IN %@", arrayOfTrims];

And here is the error I'm getting :

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (ANY models.trims IN {<Trim: 0x8e60340> (entity: Trim; id: 0x8e62f10 <x-coredata://BCD28560-AED5-4409-9A35-1925001773E6/Trim/p8>

I'm kinda new to Core Data and I have no idea what I'm doing wrong.

Hope someone can help,

Thanks a lot.

like image 282
Ticko Avatar asked Feb 28 '14 16:02

Ticko


2 Answers

"ANY" in a Core Data predicate works only for a single to-many relationship. Since your query involves two to-many relationships, you have to use a SUBQUERY:

[NSPredicate predicateWithFormat:@"SUBQUERY(models, $m, ANY $m.trims IN %@).@count > 0",
    arrayOfTrims];
like image 172
Martin R Avatar answered Sep 20 '22 17:09

Martin R


This exception is also raised if one of the predicates uses a column (i.e. field) name that does not exist. Totally misleading error message...

like image 29
Markus Avatar answered Sep 18 '22 17:09

Markus