Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nested NSPredicate SUBQUERY

This is the relevant part of my object graph:

Store <-->> Gift <<--> Person <<-->> Tag

I'd like to be able to filter the stores by the tag.filtering attribute, like this:

ANY gifts.person.tags.filtering == YES

But I understand why this doesn't work, due to the restriction to one to-many keys. So I am trying to use a set of nested SUBQUERY predicates, like this:

NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:
   @"(0 != SUBQUERY(gifts, $x, 
     (0 != SUBQUERY($x.person.tags, $y, $y.filtering==YES).@count)).@count)"];

This fails at runtime with an error:

"SQLite error code:1, 'no such column: t3.ZFILTERING'"

It seems like I'm on the right track, here, but can't find any other examples that use the same syntax. What am I missing?

like image 462
Matt Martel Avatar asked Mar 21 '23 19:03

Matt Martel


1 Answers

From my comment above:

[NSPredicate predicateWithFormat:@"SUBQUERY(gifts, $x, ANY $x.person.tags.filtering == YES).@count != 0"]

works, and is even simpler than a nested SUBQUERY.

like image 197
Martin R Avatar answered Apr 01 '23 20:04

Martin R