I am trying to migrate my Core Data model to a new version with a new attribute added to it. Since my app supports macOS 13 I am not able to use the newly introduced Staged migrations.
After much digging I found that the app is not able to find the Mapping Model when one of the attribute has "Preserve after deletion" enabled.
I have enabled migration debbuging using
com.apple.CoreData.MigrationDebug 1
I am getting following error
error: CoreData: error: (migration) migration failed with error Error Domain=NSCocoaErrorDomain Code=134140 "Persistent store migration failed, missing mapping model."
What is the way out here?
You made it clear that you probably couldn't move away from custom mapping models, but for the awareness of other folks, I'd still mention that using custom mapping models is now discouraged, as discussed here.
To to comment your question, every time you change your model version, you need to regenerate the model mapping file so the version hashes are matched. You can do so by executing Xcode File > New > File From Tempdate... menu, pick Core Data > Mapping Model in the new file template sheet and then follow the guide to finish that.
If you do re-create the mapping model after changing the model, but still see the “can't find mapping model for migration" error, it may be that Xcode fails to create the mapping model with the right hashes, which will be an Xcode bug.
In that case, you can probably work around the issue by tweaking the mapping model with your own code. To do that:
-
Figure out the version hashes of the entities in your source and destination Core Data model versions. You can use Core Data APIs (NSManagedObjectModel > NSEntityDescription > versionHash) to dump all the hashes.
-
Set sourceEntityVersionHash and destinationEntityVersionHash for the entity mappings in your mapping model.
-
Use migrateStore(from:type:options:mapping:to:type:options:) to trigger the migration.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.