CoreData+CloudKit Model Migration Failing

So I've been developing happily with CoreData+CloudKit for a while now, and really haven't run into any issues until this. I've migrated my model a couple of times, and it worked fine, but this was the first time I added a new entity to the model. If I run on a device (any device, iPhone, iPad, Mac via Catalyst), I get a migration error. Here's the entirety of my persistentContainer setup:


    public var persistentContainer: NSPersistentContainer = {
        let modelPath = Bundle(for: CoreDataStack.self).url(forResource: "Progress", withExtension: "momd")!
        let model = NSManagedObjectModel(contentsOf: modelPath)!
        let container = NSPersistentCloudKitContainer(name: "Progress", managedObjectModel: model)
                
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                print(storeDescription)
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()


And here's the error I'm getting:C

allstacks=true}}}

CoreData: annotation: : Attempting recovery from error encountered during addPersistentStore: Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=[…].sqlite, reason=Cannot migrate store in-place: constraint violation during attempted migration, destinationURL=[…].sqlite, NSUnderlyingError=0x600000cebae0 {Error Domain=NSCocoaErrorDomain Code=134111 "(null)" UserInfo={_NSCoreDataOptimisticLockingFailureConflictsKey=(
), NSUnderlyingException=Constraint unique violation, reason=constraint violation during attempted migration, NSExceptionOmitCallstacks=true}}}

Important note: I am not using contraints at all in this model. I have created a new Entity, given it a single attribute, and a single one-to-many relationship with an existing entity. That's all.


Also, here's an excerpt from the logs when I have -com.apple.CoreData.MigrationDebug enabled, pointing to an issue with the CloudKit metadata tables update step:


CoreData: annotation: Completed persistent history metadata tables update
CoreData: annotation: Beginning CloudKit metadata tables update
CoreData: annotation: Failed lightweight migration on connection
CoreData: annotation: Rolling back formal transaction


Anyone seen this, have any idea what could be failing, or how to make cloudkit/coredata happy with a migration like this?

I have exatly the same issue as you. In addition to the MigrationDebug I also enabled SQLDebug. I receive the following log messages:

CoreData: annotation: Completed persistent history metadata tables update
CoreData: annotation: Beginning CloudKit metadata tables update
CoreData: sql: UPDATE ANSCKRECORDMETADATA SET ZENTITYID = 4 WHERE ZENTITYID = 3
CoreData: annotation: Failed lightweight migration on connection
CoreData: annotation: Rolling back formal transaction
CoreData: sql: ROLLBACK
CoreData: annotation: Disconnecting from sqlite database.
CoreData: annotation: Disconnecting from sqlite database.


I created a TSI for this issue and will you now if I have any news.

Thanks @kevdek. Glad to know I'm not alone.
Ok I figured something out, and this line in kevdek's answer tipped me off:

Code Block
CoreData: sql: UPDATE ANSCKRECORDMETADATA SET ZENTITYID = 4 WHERE ZENTITYID = 3

I found similar logs in my debugging. It appears that CoreData wants *really badly* to have the entity IDs based on the entity names, sorted alphabetically. As such, if your new entity results in the shuffling of entity IDs, it seems that CloudKit fails the migration.

On a hunch, I renamed my new entity from "Reminder" to "ZReminder", and the migration was successful.

Surely there is a better solution.

Surely there is a better solution.

There is in fact. The feedback assistant can be used to file bugs. Include both versions of your model and the store files that reproduce this issue.
Is there any update on this?
Same issues as rest of thread. Seems like https://developer.apple.com/forums/thread/131981? is a similar issue. Please reply if you have any updates.

I just figured out that this issue can occur when CD_entityName indexes aren't available or deleted (SORTABLE, SEARCHABLE, QUERYABLE). Adding them might help (at least for me).

I just finished fixing the same issue with my app. I had the exact same issue. The app worked fine when running from the simulator, but crashed with the lightweight migration error when running from a tethered device. In my debug output, noticed that the "file:///var/mobile/Containers/..." path indicated that the model file was saved to an App Group (I share data with a widget extension). In order to fix the issue, I created a new App Group and this fixed the problem.

Did anyone have success with this? I'm currently facing the same issue in a project.

First, my feedback number: FB13329733.

I took one of my DTS tickets and requested some help on this issue. This seems to be an actual bug in CoreData:

Every Core Data CloudKit store has several tables to store the synchronization state, and ANSCKRECORDMETADATA is one of them. It stores the maps between Core Data entities and CloudKit record types, and has a unique constraint on its ZENTITYID and ZENTITYPK attributes.

When migrating an existing store, Core Data analyzes the source and destination model versions, and updates the tables so they reflect the new model version. The constraint conflict, I reported, was triggered in the process.

The issue doesn’t happen when I use NSPersistentContainer because Core Data doesn’t migrate the tables when CloudKit doesn’t get involved.

The migration process is completely internal to Core Data, and there is no supported way for developers to impact the behavior.

The DTS engineer couldn't provide me with more details except the notice that the issue has been forwarded to the CoreData team.

CoreData+CloudKit Model Migration Failing
 
 
Q