Fix: Core Data Model Incompatible Error in Xcode

Written by

in

Quick answer: This error occurs when your Core Data model changes don’t match the persistent store. Fix it by creating a new model version in your .xcdatamodeld file, setting it as current, and enabling lightweight migration with NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption in your persistent store options.

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.

  1. Open your .xcdatamodeld file in Xcode.
  2. In the menu bar, choose Editor → Add Model Version….
  3. Name the new version (e.g., ModelV2) and base it on the current model.
  4. Select the .xcdatamodeld parent file, open the File Inspector (⌘⌥1), and under Versioned Core Data Model choose the new version as Current.
  5. Make your schema changes (add entity, attribute, relationship, etc.) in the new version.
  6. 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:

  1. Stop the app in Xcode.
  2. Delete the app from the simulator/device (long-press → Remove App).
  3. In Xcode, choose Product → Clean Build Folder (⇧⌘K).
  4. 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:

  1. File → New → File → Core Data → Mapping Model.
  2. Choose the source (old) and destination (new) model versions.
  3. Customize entity/attribute mappings in the mapping model editor.
  4. 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.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.