An error occurring during Core Data persistent store migration in iOS 13

After updating XCode to version 11 I added a new model version to Core Data and in new version I added a new attribute to an Entity. Made the new version active and added the new property to managed object file.

After releasing this version to the users it started to crash with the following message: "The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store." and "duplicate column name ZNEWCOLUMN". Until now I made a lot of changes to the Core Data model and migration always worked.

This crash appears only on iOS 13!

This is how I load Core Data:


    lazy var managedObjectContext: NSManagedObjectContext = {
       
        return self.persistentContainer.viewContext
    }()


    private lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
         */


        let container = NSPersistentContainer(name: "MyModel")
        container.persistentStoreDescriptions.append(storeDescription)
       
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
           
            if let error = error as NSError?
            {
                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()
   
    private lazy var storeDescription: NSPersistentStoreDescription = {
        let storeDescription = NSPersistentStoreDescription()
        storeDescription.shouldMigrateStoreAutomatically = true
        storeDescription.shouldInferMappingModelAutomatically = true
        return storeDescription
    }()

Replies

The same thing is happening to me, migrating at iOS 12 was right at real device and Simulator but at iOS 13 fail with the next log result:

SQLite error code:1, 'duplicate column name: ZNAME_OF_THE_COLUMN .... Error Domain = NSCocoaErrorDomain Code = 134110 "An error occurred during persistent storage migration."

I check the xxxx.sqlite database file in the emulator path before and after the migration and there were no columns with those new same names. To know the route of the *.sqlite in the emulator you have to put a breakpoint and when it is stopped put in the console

po NSHomeDirectory()
. Then go to Finder window, tap the keys
Control + Command + G
and paste the route. Yo can handle it (for example) with DB Browser for SQLite program, it´s free.


After a long search I have seen what has happened to some people but I have not seen any solution.

Mine was:


  1. Select the actual *.xcdatamodel.
  2. Select Editor > Add Model Version.
  3. Provide a version name based on the previous model (like XxxxxxV2.xcdatamodel).
  4. Click on this new version model NewV2.xcdatamodel.
  5. Select this new version as Current on Properties at right hand of IDE.
  6. Make your changes at DDBB.
  7. Run app and will work fine.


I did tests overriding app (with new values) and it was fine.

I hope this may help.