title: Adding a relationship in core data
body: <p>I have been at this single task for several days trying to get the relationships between core data entities working. I have achieved this but now I need to change it so that the new attribute value has its relationship added to an existing object. It is a 1 - to - many database.</p>
  
  <p>I am not sure how to add a relationship to a object that already exists. So in the new object that is getting added to RoutineDetail, how would I create the relationship to the object that already exists in the routine Entity?</p>
  
  <p>I have looked at several examples all showing how to add relationships to newly added objects but I need it so the new object in <code>RoutinesDetails</code> has a relationship with the value that already exists in <code>Routines</code>.</p>
  
  <p>The value of <code>Routines</code> is held in a string called <code>RoutineText</code></p>
  
  <pre><code>rout is the NSmangedObject for the entity Routines
  
  routDet is the NSmanagedObject for the entity RoutinesDetails
  </code></pre>
  
  <p>I have left the commented out code that allows me to add a relationship when both new objects are created.</p>
  
  <p>This is the last thing I have to do in my project but it is driving me insane. I will be eternally grateful for the fix here. Any advice will be appreciated as this is the best knowledge portal there is. Thank You.</p>
  
  <pre><code> NSManagedObjectContext *context = [self managedObjectContext];
  
      // Create a new device
      ExcerciseInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];
  
  //rout = [NSEntityDescription insertNewObjectForEntityForName:@"Routines" inManagedObjectContext:context];
  
      routdet = [NSEntityDescription insertNewObjectForEntityForName:@"RoutinesDetails" inManagedObjectContext:context];
  
      //Add attribute values
      //[rout setValue: RoutineText  forKey:@"routinename"];
      [routdet setValue: info.name  forKey:@"image"];
  
      //Create Relationship
      [rout addRoutinedetObject:routdet];
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/QtpFk.png" alt="enter image description here"></p>