Sean Danzeiser
Sean Danzeiser

Reputation: 9243

Core Data Migration from Integer 16 to Transformable

In my application, I have a core data entity with an attribute, value. I would like to change the type of this attribute from Integer 16 to Transformable in my upcoming release.

Now, when setting up my persistent store coordinator, I include the following as options:

let options: [NSString: Bool] = [
            NSMigratePersistentStoresAutomaticallyOption: true,
            NSInferMappingModelAutomaticallyOption: true]

try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options) ...

Now, I've created a second version of my core data model which includes the update to the value attribute, and no other changes. When I attemp to build, I get the following errors:

CoreData: error: -addPersistentStoreWithType:SQLite ... Persistent store migration failed, missing mapping model

and further down, this:

Can't find or automatically infer mapping model for migration ... Source and destination attribute types are incompatible.

So apparently CoreData can't map from the Integer 16 to Transformable automatically? How can I go about resolving this issue?

Upvotes: 3

Views: 827

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70946

There's no obvious rule for how to convert an existing integer value to a binary blob, so Core Data throws up it's hands here and says it doesn't know what you want it to do. Converting an attribute to a different data type requires more work on your part.

If you are actually converting data-- as in, you want the new binary value to be somehow derived from the old integer value-- then you need to create a mapping model and do migration the relatively-hard way. Part of this will involve you making the integer-to-binary conversion. There's a lot to know here so I'll refer you to Apple's Core Data migration guide for more information.

However if the new binary value is independent of the old integer value-- meaning that the new data is not derived from the old data but is a complete replacement for it-- you can keep things simple. If that's the case, don't rename the attribute. Create a new attribute with a different name, valueBlob or whatever, and delete the value attribute. Automatic lightweight migration can handle adding and removing attributes, so it could handle that change.

Upvotes: 1

Related Questions