Core Data Migration error when migrating between non-consecutive versions
Asked Answered
D

1

6

The Problem

  • I've got 13 versions of my core data model.
  • I've made 13 mapping models (V1-V2, V2-V3 etc)
  • I've got automatic migration switched on.
  • On migration between two consecutive versions (e.g. V12-V13) the migration works perfectly
  • On migration between two non-consecutive versions (e.g. V11-V13) the migration fails with the error:

    Can't find mapping model for migration

What I've tried

Creating a mapping model for every possible combination of versions. This works, but it's a real hassle.

For version 14 I'll need to make 14 different mapping models. Manually. Ugh.

Code

Here's the options I pass in for migration:

[persistentStoreCoordinator addPersistentStoreWithType:[self storeType]
                                          configuration:nil 
                                                    URL:url 
                                                options:[NSDictionary dictionaryWithObjectsAndKeys:
                                                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                                         [NSNumber numberWithBool:NO], NSReadOnlyPersistentStoreOption,
                                                         nil]
                                                  error:&error]){

Data Model

My data model is pretty complex, but here it is: http://dl.dropbox.com/u/136780/Engine_V2_DataModel.xcdatamodeld.zip

I'm not using any version hash modifiers or renaming identifiers in my data model right now.

Destrier answered 8/4, 2011 at 11:12 Comment(2)
Why have mapping models V1-V2, V2-V3, V3-V4 et.c. when you can have V1-V14, V2-V14, V3-V14 et.c? The latter allows you to do the migration in a single step.Bearskin
I'm aiming for the V1-V2, V2-V3 etc setup because it's much easier. It's not fun to manually make 14 mapping models whenever you want to make one migration. In addition, all the docs I've seen imply that V1-V2, V2-V3 etc is the official way to migrate stores. I feel like I'm doing something wrong.Destrier
B
8

See this Stack Overflow question: Core Data Migration Across Multiple Version Upgrades

Basically, the gist is that you don't need to create the complete set of mapping models. You just need mapping models between each pair of successive models, and then you can progressively migrate any old model through the intermediate models to the newest model.

The performance may not be good depending on your store, because you're not doing the migration in one single step. It may be good enough, though, and may be worth it to not have to create the complete set of mapping models. You can also test out the performance of this method to determine whether you need to create maps between non-successive versions of your model.

Marcus Zarra (Core Data aficionado extraordinaire) has the code here: http://media.pragprog.com/titles/mzcd/code/ProgressiveMigration/AppDelegate.m

(I was just about to ask this question, but thanks to LazyTwitter I was pointed to this answer by Aaron Tuller: https://twitter.com/tullera/status/57708304683642880 )

Briquet answered 12/4, 2011 at 7:52 Comment(4)
Simone, Thanks so much for this. I'm in shock that I'm actually doing it right. Since I have a huge amount of data, a single migration can take a couple of minutes, so migrating in stages isn't practical for me. It looks like multiple mappings are the only way of going. Rubbish!Destrier
Macrus Zarra's code is great, and I highly recommend his book on Core Data. That said, I had to make the following change to it to prevent it from recursing infinitely if it discovers the .mom files in the wrong order: gist.github.com/2321704Baskin
3 years later... Is this still the best solution for this?Kirov
I'm 95% sure there hasn't been any change here even for the latest version of Core Data. If you want best performance and migration in just one step, you still have to make mapping models for every possible combination of versions. Note, of course, if you include the NSInferMappingModelAutomaticallyOption AS WELL AS the NSMigratePersistentStoresAutomaticallyOption option, this will save you a lot of headache. We have 21 model versions and they all have their mapping models inferred (instead of us making explicit mapping models), with no problems.Briquet

© 2022 - 2024 — McMap. All rights reserved.