plm
plm

Reputation: 198

CoreData managedObjectContext save Violates UNIQUE constraint for Z_PK

I am using CoreData in an iOS application to manage words in a "flash cards" application for people learning new languages.

The problem I'm having is that when I set up the data for a new entity and try to save it to the data store, I get a violation of a UNIQUE CONSTRAINT requirement on the sqlite database. The column in question is the Z_PK column, which I understand to be a PRIMARY KEY created by the iOS core data methods when the data store is initially created.

Here's the UNIQUE CONSTRAINT message I'm getting when I attempt the save:

2015-03-14 09:25:14.427 ghoti[25856:1107373] CoreData: error: (1555) UNIQUE constraint failed: ZGHOTIENTITY.Z_PK (lldb)

Z is the prefix stuck on all of these SQL columns
GHOTIENTITY is my data store
Z_PK is the primary key

Here's a quick screenshot of the data table inside the sqlite "editor" in firefox: datastructure from sqlite

The definition of the business object entity itself does not include the Z_PK column:

@implementation ghotiEntity

@dynamic numberCorrect;
@dynamic numberIncorrect;
@dynamic likelihoodOfPresentingWord;
@dynamic englishText;
@dynamic hebrewText;
@dynamic phoneticText;

@end

I'd be annoyed - but compliant - if it were as simple as just setting the ghotiEntity.Z_PK to the maximum value of Z_PK in the datastore + 1, but that column is not even part of the entity definition in the first place.


EDIT - A helpful user asked for the code, so I'm adding it here rather than trying to stuff it into a comment:

I'm using the mmBusinessObject collection of core data methods that I've seen floating around the web. Kevin McNeish explains it fully here.

The basic structure is this:

mmBusinessObject.m and .h do a nice job of wrapping the managedObject methods into something "easy to read and deal with". For the sake of readability, I've copied the snippet of two elements [entity createEntities] and [entity saveEntities] here and the full .m file at the end of this post. (note that I have not yet built in all of the error-checking, I'm just trying to make the basic function work.

CREATE A NEW ENTITY:

// Creates a new entity of the default type and adds it to the managed object context
- (NSManagedObject *)createEntity
{
    return [NSEntityDescription insertNewObjectForEntityForName:self.entityClassName inManagedObjectContext:[self managedObjectContext]];
}

SAVE THE CHANGES

- (void)saveEntities
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

So my operating code (inside my ViewController.m file) does this: (note that ghotiEntity is my specific business object.

- (IBAction)saveButtonAction:(UIButton *)sender {

    ghotiEntity *newGhotiEntry = (ghotiEntity *)[ghotiData createEntity];
    newGhotiEntry.englishText=self.englishWord.text;
    newGhotiEntry.hebrewText=self.hebrewWord.text;
    newGhotiEntry.phoneticText=self.phoneticWord.text;
    newGhotiEntry.numberCorrect=0;
    newGhotiEntry.numberIncorrect=0;
    newGhotiEntry.likelihoodOfPresentingWord=0;

    [ghotiData saveEntities];

    [self enableRegularUI];
    [self pickNextCard];
}

It is from the NSLog line in the saveEntities method above that I get the diagnostic information that it is the Z_PK column that has the UNIQUE CONSTRAINT problem.


As promised above, here is the full .m file. I need to make sure that it is clear that the credit for this code goes to either Kevin McNeish as noted above, or any of several other collaborators who have posted the same code in other places... just search mmBusinessObject in google. I think Kevin is the originator and his tutorial is really good. The first few lines in the versions I have seen all have Kevin's name in them!

#import "mmBusinessObject.h"

@implementation mmBusinessObject

// Initialization
- (id)init
{
    if ((self = [super init])) {
        _copyDatabaseIfNotPresent = YES;
    }
    return self;
}

// Creates a new entity of the default type and adds it to the managed object context
- (NSManagedObject *)createEntity
{
    return [NSEntityDescription insertNewObjectForEntityForName:self.entityClassName inManagedObjectContext:[self managedObjectContext]];
}

// Delete the specified entity
- (void) deleteEntity:(NSManagedObject *)entity {
    [self.managedObjectContext deleteObject:entity];
}

// Gets entities for the specified request
- (NSMutableArray *)getEntities: (NSString *)entityName sortedBy:(NSSortDescriptor *)sortDescriptor matchingPredicate:(NSPredicate *)predicate
{
    NSError *error = nil;

    // Create the request object
    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    // Set the entity type to be fetched
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]];
    [request setEntity:entity];

    // Set the predicate if specified
    if (predicate) {
        [request setPredicate:predicate];
    }

    // Set the sort descriptor if specified
    if (sortDescriptor) {
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
        [request setSortDescriptors:sortDescriptors];
    }

    // Execute the fetch
    NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    if (mutableFetchResults == nil) {

        // Handle the error.
    }

    return mutableFetchResults;
}

// Gets all entities of the default type
- (NSMutableArray *)getAllEntities
{
    return [self getEntities:self.entityClassName sortedBy:nil matchingPredicate:nil];
}

// Gets entities of the default type matching the predicate
- (NSMutableArray *)getEntitiesMatchingPredicate: (NSPredicate *)predicate
{
    return [self getEntities:self.entityClassName sortedBy:nil matchingPredicate:predicate];
}

// Gets entities of the default type matching the predicate string
- (NSMutableArray *)getEntitiesMatchingPredicateString: (NSString *)predicateString, ...;
{
    va_list variadicArguments;
    va_start(variadicArguments, predicateString);
    NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString
                                                    arguments:variadicArguments];
    va_end(variadicArguments);
    return [self getEntities:self.entityClassName sortedBy:nil matchingPredicate:predicate];
}

// Get entities of the default type sorted by descriptor matching the predicate
- (NSMutableArray *)getEntitiesSortedBy: (NSSortDescriptor *) sortDescriptor
                      matchingPredicate:(NSPredicate *)predicate
{
    return [self getEntities:self.entityClassName sortedBy:sortDescriptor matchingPredicate:predicate];
}

// Gets entities of the specified type sorted by descriptor, and matching the predicate string
- (NSMutableArray *)getEntities: (NSString *)entityName
                       sortedBy:(NSSortDescriptor *)sortDescriptor
        matchingPredicateString:(NSString *)predicateString, ...;
{
    va_list variadicArguments;
    va_start(variadicArguments, predicateString);
    NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString
                                                    arguments:variadicArguments];
    va_end(variadicArguments);
    return [self getEntities:entityName sortedBy:sortDescriptor matchingPredicate:predicate];
}

- (void) registerRelatedObject:(mmBusinessObject *)controllerObject
{
    controllerObject.managedObjectContext = self.managedObjectContext;
}

// Saves all changes (insert, update, delete) of entities
- (void)saveEntities
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

#pragma mark -
#pragma mark Core Data stack

/**
 Returns the managed object context for the application.
 If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
 */
- (NSManagedObjectContext *)managedObjectContext {

    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}

/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created from the application's model.
 */
- (NSManagedObjectModel *)managedObjectModel {

    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:self.dbName withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    // If the sqlite database doesn't already exist, create it
    // by copying the sqlite database included in this project

    if (self.copyDatabaseIfNotPresent) {

        // Get the documents directory
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        NSString *docsDir = paths[0];

        // Append the name of the database to get the full path
        NSString *dbcPath = [docsDir stringByAppendingPathComponent:
                             [self.dbName stringByAppendingString:@".sqlite"]];

        // Create database if it doesn't already exist
        NSFileManager *fileManager = [NSFileManager defaultManager];

        if (![fileManager fileExistsAtPath:dbcPath]) {
            NSString *defaultStorePath = [[NSBundle mainBundle]
                                          pathForResource:self.dbName ofType:@"sqlite"];
            if (defaultStorePath) {
                [fileManager copyItemAtPath:defaultStorePath toPath:dbcPath error:NULL];
            }
        }



    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:
                       [NSString stringWithFormat:@"%@%@", self.dbName, @".sqlite"]];

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

         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:
         [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

         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(@"%@",storeURL);
        if ([error code] == 134100) {
            [self performAutomaticLightweightMigration];
        }
        else
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
    return _persistentStoreCoordinator;
}

- (void)performAutomaticLightweightMigration {

    NSError *error;

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", self.dbName, @".sqlite"]];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                   configuration:nil
                                                             URL:storeURL
                                                         options:options
                                                           error:&error]){
        // Handle the error.
    }
}


#pragma mark -
#pragma mark Application's Documents directory

/**
 Returns the URL to the application's Documents directory.
 */
- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

@end

Upvotes: 3

Views: 5236

Answers (3)

Joe Coder
Joe Coder

Reputation: 21

I had the same problem with a Z_PK constraint issue. It stemmed from the fact that I had copied the empty sqlite database from my app's documents folder and prepopulated it using Firefox's SQLite manager add-in. The step I forgot was updating the Z_PrimaryKey table...you have to update the Z_MAX value for each table so that Core Data will know the next Z_PK to use when new records are added to the database. Simply set each Z_Max value with the number of records in each table. Save it and drop it back into your app and all will be well.

Upvotes: 2

Constantin Saulenco
Constantin Saulenco

Reputation: 2383

i had the same issue but i had this when i want to update an entry , so i fixed with refreshObject then save like this:

[dataManager.managedObjectContext refreshObject:object mergeChanges:YES];
[dataManager saveContextWithBlock:block];

Upvotes: 0

Escape Velocity Apps
Escape Velocity Apps

Reputation: 64

Look through your code for all places you create an object for the DB. Make sure that you are not creating objects from multiple threads.

Upvotes: 1

Related Questions