16

Could any one tell me what's the wrong with this code? It raises the following error and cause application to crash:

reason: 'keypath Studies.patients.PatientName not found in entity <NSSQLEntity Studies id=3>'

Code:

 - (void)viewDidLoad {
        [super viewDidLoad];

        test_coredataAppDelegate *appDelegate = (test_coredataAppDelegate *)[[UIApplication sharedApplication] delegate];
        self.context = appDelegate.managedObjectContext;


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription 
                                       entityForName:@"Studies" inManagedObjectContext:_context];
        [fetchRequest setEntity:entity];
        /**/
        NSLog(patientName);
        [fetchRequest setPredicate:[NSPredicate predicateWithFormat:
                               @"(Studies.patients.PatientName == %@ )",patientName]];



        NSError *error;
        self.StudiessList = [_context executeFetchRequest:fetchRequest error:&error];
        self.title = @"patients"; 
        [fetchRequest release];

    }
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Ali
  • 1,975
  • 5
  • 36
  • 55

4 Answers4

18

Firstly, since your fetch entity is Studies you don't include it in the predicate because the Studies objects are the ones receiving the predicate test in the first place. So your predicate should be at least just:

patients.PatientName == %@

However, by convention, patients would indicate a to-many relationship. If so, that means that the actual value of patients is a set of (presumably) Patient objects. As such you can't ask a set for an attribute value as above: Instead you have to ask for a new set of all object in the set that match the predicate. Use the ANY or All operator like so:

ALL patients.PatientName == %@

I would add that by convention all attribute and relationship names start with lower case letters so if PatientName is an attribute it should be patientName.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • It sounds like you need ANY then. You seem to be looking for "any Studies where ANY patients.patientName equals the variable." In that case, the ALL would not be appropriate. The all would only work if all the patients.patientName had the same name. – TechZen Mar 08 '11 at 22:00
6

Either the Studies entity does not have a patient property, or whatever entity the patients relationship points to does not have a PatientName property (pay attention to upper/lowercase issues) or both.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
1

Usually when I see this error it means that I added a new Version to my Model but forgot to update the Current Model Version for the project.

Screenshot of Xcode property

Posting this answer to help my future self because it's not the first time I have landed on this page!

rodhan
  • 633
  • 7
  • 12
0

In my case, I was referencing a superclass property of the entity but hadn't set the parent entity in the xcdatamodel file. I had to select the other NSManagedObject which I was subclassing from the inspector menu (default is None):

xcdatamodel snippet

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71