title: CoreData and the Foreign Keys from database server
body: <p>I have a problem concerning the CoreData and the synchronisation with a server database. When I synchronize two entities with two table (RIGHTS and CUSTOMERS) on the server, the problem is that if there is a foreign key on the server, how can I manage it ? For example :
  <a href="https://i.stack.imgur.com/vVO4Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vVO4Z.png" alt="enter image description here"></a></p>
  
  <p><strong>//CUSTOMERS SYNCHRONIZATION</strong></p>
  
  <pre><code>    NSLog(@"Value : %@", [[jsonDict valueForKey:@"Customers"] objectAtIndex:0]);
      for (id object in [[jsonDict valueForKey:@"Customers"] objectAtIndex:0])
      {
          NSLog(@"%@", object);
          // if jsonDict.customers.id_customer pas présent dans table Customers
          //Recherche id_customer dans CoreData
          NSError *error = nil;
  
          NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id_customer = %@", [object valueForKey:@"id_customer"]]; //create a filter
          NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Customers"]; //create a query
          request.predicate = predicate; //set the filter on the query
          NSArray *customerSearch = [[self managedObjectContext] executeFetchRequest:request error:&amp;error]; //execute the query
  
          if ([customerSearch count] == 0)
          {
              NSManagedObject *customers = [NSEntityDescription
                                            insertNewObjectForEntityForName:@"Customers"
                                            inManagedObjectContext:context];
              [customers setValue:[NSNumber numberWithInt:[[object valueForKey:@"id_customer"] intValue]] forKey:@"id_customer"];
              [customers setValue:[object valueForKey:@"address"] forKey:@"address"];
              [customers setValue:[object valueForKey:@"firstname"] forKey:@"firstname"];
              [customers setValue:[object valueForKey:@"lastname"] forKey:@"lastname"];
          [customers setValue:[NSNumber numberWithInt:[[object valueForKey:@"id_right"] intValue]] forKey:@"id_right"];//Foreign Key problem
          }
          else if ([customerSearch count] == 1)
          {
              //Update the object
              Customers *customers = [[context executeFetchRequest:request error:&amp;error] lastObject];
  
              customers.address = [object valueForKey:@"address"];
              customers.firstname = [object valueForKey:@"firstname"];
              customers.lastname = [object valueForKey:@"lastname"];
              customers.id_right = [NSNumber numberWithInt:[[object valueForKey:@"id_right"] intValue]];//Foreign Key problem
          }
          else
          {
              NSLog(@"WARNING - May be problems with duplicate id_customer in database"); //if &gt; 1 then problem
          }
  
          if (![context save:&amp;error])
          {
              NSLog(@"Whoops, couldn't save Customers: %@", [error localizedDescription]);
          }
      }
  </code></pre>
  
  <p><strong>//RIGHTS SYNCHRONIZATION</strong></p>
  
  <pre><code>NSLog(@"Value : %@", [[jsonDict valueForKey:@"Rights"] objectAtIndex:0]);
  for (id object in [[jsonDict valueForKey:@"Rights"] objectAtIndex:0])
  {
      NSLog(@"%@", object);
      // if jsonDict.customers.id_customer pas présent dans table Customers
      //Recherche id_customer dans CoreData
      NSError *error = nil;
  
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id_right = %@", [object valueForKey:@"id_right"]]; //create a filter
      NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Rights"]; //create a query
      request.predicate = predicate; //set the filter on the query
      NSArray *customerSearch = [[self managedObjectContext] executeFetchRequest:request error:&amp;error]; //execute the query
  
      if ([customerSearch count] == 0)
      {
          NSManagedObject *rights = [NSEntityDescription
                                        insertNewObjectForEntityForName:@"Rights"
                                        inManagedObjectContext:context];
          [rights setValue:[NSNumber numberWithInt:[[object valueForKey:@"id_right"] intValue]] forKey:@"id_right"];
          [rights setValue:[object valueForKey:@"name"] forKey:@"name"];
      }
      else if ([customerSearch count] == 1)
      {
          //Update the object
          Rights *rights = [[context executeFetchRequest:request error:&amp;error] lastObject];
  
          rights.name = [object valueForKey:@"name"];
      }
      else
      {
          NSLog(@"WARNING - May be problems with duplicate id_customer in database"); //if &gt; 1 then problem
      }
      if (![context save:&amp;error])
      {
          NSLog(@"Whoops, couldn't save Rights : %@", [error localizedDescription]);
      }
  }
  </code></pre>
  
  <p>If the answer is that I have to manage that by writing the fact that it is a relationship again on the iPad, I am sorry to say that CoreData seems to give more work than the classic SQL management with SQLite. 
  If I am not wrong:</p>
  
  <ul>
  <li><p>With the SQL, you juste have to download your tables from the server
  containing the foreign keys, and it is done.</p></li>
  <li><p>With the CoreData, you have to manage the fact that if it is a
  foreign key, you have to create relationship.</p></li>
  </ul>
  
  <p><strong>EDIT :</strong> 
  For your information:
  Conceptual Data Model :
  <a href="https://i.stack.imgur.com/OaBDh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OaBDh.png" alt="enter image description here"></a>
  I used Sqlite doing my own tables, and I copy directly the data into them from the server (retrieving the primary keys, foreign keys). It is ten times better than using CoreData. I used DB Browser for SQLite to create my schéma one time, and that's it. No need to have 2000 lines of code to write about relationships that complicate the application.</p>
  
  <p><a href="https://i.stack.imgur.com/LMhGD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LMhGD.png" alt="enter image description here"></a></p>
  
  <p><strong>Conclusion : I give up CoreData!</strong></p>