title: IOS Coredata UNIQUE constraint failed:
body: <p>I am trying to insert data into coredata but i am getting error like ,</p>
  
  <blockquote>
    <p>CoreData: error: (1555) UNIQUE constraint failed: ZSIDETABLENAME.Z_PK</p>
  </blockquote>
  
  <p>Scenario :-</p>
  
  <p>First: from appdelegate.m I am copying data from my SQL file to document directory's sql file .</p>
  
  <pre><code>- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
  {
      if (_persistentStoreCoordinator != nil) {
          return _persistentStoreCoordinator;
      }
  
  
     //  Create the coordinator and store
  
          NSString* from;
          NSString* to;
          NSArray* mainPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [mainPath objectAtIndex:0]; // Get documents folder
          /*VoiceRecords.plist file*/
          from=[[NSBundle mainBundle]pathForResource:@"News" ofType:@"sqlite"];
          to=[documentsDirectory stringByAppendingPathComponent:@"News.sqlite"];
          [[NSFileManager defaultManager]copyItemAtPath:from
                                                 toPath:to error:nil];
          from=nil;
          to=nil;
  
      NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"News.sqlite"];
  
      NSError *error = nil;
      _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
      if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&amp;error]) {
          /*
           Replace this implementation with code to handle the error appropriately.
  
           abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  
           Typical reasons for an error here include:
           * The persistent store is not accessible;
           * The schema for the persistent store is incompatible with current managed object model.
           Check the error message to determine what the actual problem was.
  
  
           If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
  
           If you encounter schema incompatibility errors during development, you can reduce their frequency by:
           * Simply deleting the existing store:
           [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
  
           * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
           @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
  
           Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
  
           */
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          abort();
      }
  
      return _persistentStoreCoordinator;
  }
  </code></pre>
  
  <p>It works correctly. Then i am trying to save url entered by user in coredata in mainviewcontroller.m.</p>
  
  <pre><code>- (void)feedParserDidFinish:(MWFeedParser *)parser {
  
          NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"IWorld"];
                  fetchRequest.predicate = [NSPredicate predicateWithFormat:@"feedurl = %@", self.Feedlink];
          NSManagedObjectContext *context = [self managedObjectContext];
  
        if ([[context executeFetchRequest:fetchRequest error:NULL] count] == 0) {
                  NSError *error = nil;
  
              NSPredicate *predicate=[NSPredicate predicateWithFormat:@"feedurl contains %@",check];
              [fetchRequest setPredicate:predicate];
                NSMutableArray *checkp = [[context executeFetchRequest:fetchRequest error:&amp;error]mutableCopy];
  
  
              if (checkp.count&lt;=0) {
                  NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"IWorld" inManagedObjectContext:context];
                  [newDevice setValue:self.Name forKey:@"name"];
                  [newDevice setValue:self.WebsiteName forKey:@"websitename"];
                  [newDevice setValue:self.Feedlink forKey:@"feedurl"];
  
                                     // Save the object to persistent store
                  if (![context save:&amp;error]) {
                      NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
                  }
                      //Save value in lockbox
  
                  NSArray *keys = [[[newDevice entity] attributesByName] allKeys];
                  NSDictionary *dict = [newDevice dictionaryWithValuesForKeys:keys];
  
              }
  
          }
  
  
       NSFetchRequest *fetchrequestforside=[NSFetchRequest fetchRequestWithEntityName:@"Sidetablename"]; //Sidetablename is entity
      fetchrequestforside.predicate=[NSPredicate predicateWithFormat:@"feedurl = %@", self.Feedlink];
               NSManagedObjectContext *context = [self managedObjectContext];
      if ([[context executeFetchRequest:fetchrequestforside error:NULL] count] == 0) {
                      // Create a new managed object
  
                      NSError *error = nil;
  
                      NSPredicate *predicate=[NSPredicate predicateWithFormat:@"feedurl contains %@",check ]; //check is urlstring
                      [fetchrequestforside setPredicate:predicate];
                      NSMutableArray *checkp = [[context executeFetchRequest:fetchrequestforside error:&amp;error]mutableCopy];
  
  
                      if (checkp.count&lt;=0) //if coredata will find url then notstore in coredata else stored procedure
            {
  
                      NSManagedObject *newDevice1 = [NSEntityDescription insertNewObjectForEntityForName:@"Sidetablename" inManagedObjectContext:context];
  
                      if (self.Name)
                      {
                          [newDevice1 setValue:self.Name forKey:@"name"];
  
  
                      }
                      if (self.WebsiteName)
                      {
                          [newDevice1 setValue:self.WebsiteName forKey:@"websitename"];
  
                      }
  
                      if (self.Feedlink)
                      {
                          [newDevice1 setValue:self.Feedlink forKey:@"feedurl"];
  
                      }
  
  
  
  
                      NSError *error = nil;
                      if (![context save:&amp;error]) {
                              NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); // Getting error here through nslog //CoreData: error: (1555) UNIQUE constraint failed: ZSIDETABLENAME.Z_PK
  
                          }
  </code></pre>
  
  <p>Note: this all happen at first time. Second time when I will run application all work correctly. Don't know where is issue?</p>