Why this happens
Core Data stores a hash of your data model in the SQLite persistent store. When you modify the model — adding entities, attributes, or relationships — the hash no longer matches what’s in the store. Core Data then refuses to open the store and throws:
The model used to open the store is incompatible with the one used to create the store
This commonly appears after upgrading Xcode or adding new entities to an existing model. During development you can delete the app from the simulator to reset the store, but that destroys user data. For any app that has shipped (or will ship), you must migrate the store instead.
How to fix it
1. Create a new model version and enable lightweight migration
This is the standard fix for additive, non-destructive schema changes.
- Open your
.xcdatamodeldfile in Xcode. - In the menu bar, choose Editor → Add Model Version….
- Name the new version (e.g.,
ModelV2) and base it on the current model. - Select the
.xcdatamodeldparent file, open the File Inspector (⌘⌥1), and under Versioned Core Data Model choose the new version as Current. - Make your schema changes (add entity, attribute, relationship, etc.) in the new version.
- In your persistent store coordinator setup, pass migration options:
// Swift 5+
let options: [AnyHashable: Any] = [
NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true
]
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
let storeURL = persistentContainer.persistentStoreDescriptions.first?.url
try coordinator.addPersistentStore(
ofType: NSSQLiteStoreType,
configurationName: nil,
at: storeURL,
options: options
)
If you use NSPersistentContainer (iOS 10+), set the options on the store description:
let container = NSPersistentContainer(name: "MyModel")
container.persistentStoreDescriptions.first?.setOption(true as NSNumber,
forKey: NSMigratePersistentStoresAutomaticallyOption)
container.persistentStoreDescriptions.first?.setOption(true as NSNumber,
forKey: NSInferMappingModelAutomaticallyOption)
container.loadPersistentStores { _, error in
if let error = error { fatalError("Unresolved error \(error)") }
}
Lightweight migration qualifies only for these changes:
– Add or remove an attribute or relationship
– Make a non-optional property optional (or vice-versa with a default value)
– Add or remove an entity
– Rename a property or entity (set renamingIdentifier in the model inspector)
2. Delete the app during development only
If you are still in early development and have no shipped data, you can wipe the store:
- Stop the app in Xcode.
- Delete the app from the simulator/device (long-press → Remove App).
- In Xcode, choose Product → Clean Build Folder (⇧⌘K).
- Re-run the app; Core Data will create a fresh store matching the current model.
Warning: Never use this approach once users have real data. It deletes everything.
3. Implement a custom mapping model for complex changes
If your changes exceed lightweight migration limits (e.g., changing attribute types, splitting entities, complex transformations), create a mapping model:
- File → New → File → Core Data → Mapping Model.
- Choose the source (old) and destination (new) model versions.
- Customize entity/attribute mappings in the mapping model editor.
- Add the mapping model to your target; Core Data will pick it up automatically when the same migration options are enabled.
How to prevent it
- Version your model from day one. Even the first release should be a named version (
ModelV1). - Never edit a released model version. Always add a new version for changes.
- Test migration on a device with a backup of real user data before shipping.
- Keep changes lightweight when possible; plan schema additions to avoid breaking changes.
- Use
NSPersistentContainer— it simplifies migration option configuration.
FAQ
Can I rename an entity or attribute with lightweight migration?
Yes. Select the entity or attribute in the model editor, open the Data Model Inspector (⌘⌥3), and set a Renaming Identifier to the old name. Core Data will map the old name to the new one automatically.
What if I already shipped without versioning the model?
You cannot retroactively version the original model. Your only options are: ship a new model version with migration (users will lose data on first launch), or provide a one-time migration tool that reads the old store and writes a new one. Plan versioning before first release.
Does SwiftData have the same issue?
SwiftData uses Core Data underneath, so schema mismatches still occur. The fix is similar: define schema versions in your Schema macro and enable migration in the ModelContainer configuration. The principles are identical.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.