Splitting an Entity in a CoreData Migration

Slalom Consultant Greg Martin

Greg Martin is a member of the Slalom National Mobility team which incubates emerging technology solutions in areas such as mobility, user experience and alternative application hosting.

This post assumes you have experience with CoreData and are generally familiar with the Introduction to Core Data Model Versioning and Data Migration Programming Guide.

On a current client project I was recently tasked to explore CoreData migration.  Simple migrations involving the modification of existing entities are pretty straight forward and well documented, however, one case that we wanted to verify, the splitting of an entity into two related entities, was not clearly documented.  The purpose of this post is to outline how this task can be accomplished.

Person entity attributes table

We start with a simple data model with a single Person entity that has some common person attributes, including address information.

Next we add a new model version, and create a new Address entity to move the address specific attributes from the Person to this new related entity.

person address tables

The next step is to create a Mapping Model.  The default mapping that is generated when choosing version 1 as the source and version 2 as the destination gets us close to what we need, there are just a few changes.  First we need to modify the Address entity mapping to set the Source to Person which will rename the mapping to PersonToAddress.  We then remove the person relationship from the mapping.

entity mapping

And next we update the PersonToPerson entity mapping to set the value expression for the address relationship with a KeyPath of $source (this person) and a Mapping Name of PersonToAddress.

relationship mapping

And finally, just make sure your version 2 data model is set as the current model and the NSMigratePersistentStoresAutomaticallyOption is set when you add your persistent store at application launch and the migration will happen automatically if necessary.

– Greg

5 Responses to Splitting an Entity in a CoreData Migration

  1. Zach says:

    I know this post is a year old but this was a huge help. It’s simpler than I would have thought. Thanks!

  2. Greg Martin says:

    Glad to help.

  3. Davide says:

    It’s what I needed! Thanks a lot!

  4. Frank says:

    Hi Greg,
    I’m trying to do the same thing but with a bigger database. I have a entity and in a new version I want to move some of its properties to a new entity. I’ve followed all of the steps above and tried about everything but I keep getting

    Unresolved error Error Domain=NSCocoaErrorDomain Code=134140 “The operation couldn’t be completed. (Cocoa error 134140.)”

    I’ve seen about every page on the internet about migration or this error but I can’t seem to get it to work and am starting to think it won’t work without a custom policy or something. Do you (or anyone else) have any hints to how I can resolve this issue?
    Thanks in advance

  5. Jessica says:

    Hi Greg,

    in the new model schema, you have a relation between “person” and “address”. In you screen shots only one direction of the relationship has been set “R”–>”address”. Does that mean the system will automatically set up “R”–>”person” in “address” entity?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: