如何在将关系从多个更改为一个时执行迁移?

时间:2016-11-30 13:32:41

标签: ios swift core-data-migration

这就是我在Business实体的源模型中所拥有的:

enter image description here

这就是我在Business实体的去死模型中所拥有的:

enter image description here

正如您所看到的,我将关系从多个更改为一个,从users更改为user

但现在我有以下错误:

URL:file:///private/var/mobile/Containers/Shared/AppGroup/00C2A6C6-8149-4CF7-95E1-6ABD8A2B3B57/POSowner.sqlite options:{
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=file:///private/var/mobile/Containers/Shared/AppGroup/00C2A6C6-8149-4CF7-95E1-6ABD8A2B3B57/POSowner.sqlite, reason=Cannot migrate store in-place: Validation error missing attribute values on mandatory destination relationship, destinationURL=file:///private/var/mobile/Containers/Shared/AppGroup/00C2A6C6-8149-4CF7-95E1-6ABD8A2B3B57/.POSowner.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, NSUnderlyingError=0x170458990 {Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={entity=Business, attribute=user, reason=Validation error missing attribute values on mandatory destination relationship}}} with userInfo dictionary {
    NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=134110 \"An error occurred during persistent store migration.\" UserInfo={entity=Business, attribute=user, reason=Validation error missing attribute values on mandatory destination relationship}";
    destinationURL = "file:///private/var/mobile/Containers/Shared/AppGroup/00C2A6C6-8149-4CF7-95E1-6ABD8A2B3B57/.POSowner.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3";
    reason = "Cannot migrate store in-place: Validation error missing attribute values on mandatory destination relationship";
    sourceURL = "file:///private/var/mobile/Containers/Shared/AppGroup/00C2A6C6-8149-4CF7-95E1-6ABD8A2B3B57/POSowner.sqlite";
}
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=file:///private/var/mobile/Containers/Shared/AppGroup/00C2A6C6-8149-4CF7-95E1-6ABD8A2B3B57/POSowner.sqlite, reason=Cannot migrate store in-place: Validation error missing attribute values on mandatory destination relationship, destinationURL=file:///private/var/mobile/Containers/Shared/AppGroup/00C2A6C6-8149-4CF7-95E1-6ABD8A2B3B57/.POSowner.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, NSUnderlyingError=0x170458990 {Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={entity=Business, attribute=user, reason=Validation error missing attribute values on mandatory destination relationship}}}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.58.6/src/swift/stdlib/public/core/ErrorType.swift, line 178
2016-11-30 14:25:43.070185 POSowner[3474:513840] fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=file:///private/var/mobile/Containers/Shared/AppGroup/00C2A6C6-8149-4CF7-95E1-6ABD8A2B3B57/POSowner.sqlite, reason=Cannot migrate store in-place: Validation error missing attribute values on mandatory destination relationship, destinationURL=file:///private/var/mobile/Containers/Shared/AppGroup/00C2A6C6-8149-4CF7-95E1-6ABD8A2B3B57/.POSowner.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, NSUnderlyingError=0x170458990 {Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={entity=Business, attribute=user, reason=Validation error missing attribute values on mandatory destination relationship}}}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.58.6/src/swift/stdlib/public/core/ErrorType.swift, line 178

我不知道如何解决这个问题。有什么想法吗?

这就是我设置核心数据堆栈的方式:

static func mr_setupCoreDataStack() {

    guard NSPersistentStoreCoordinator.mr_default() == nil else {
        return
    }

    let managedObjectModel = NSManagedObjectModel.mr_default()
    let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel!)

    var storePath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: SharedGroupName)
    storePath = storePath!.appendingPathComponent("POSowner.sqlite")

    let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]

    try! persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storePath, options: options)

    NSPersistentStoreCoordinator.mr_setDefaultStoreCoordinator(persistentStoreCoordinator)
    NSManagedObjectContext.mr_initializeDefaultContext(with: persistentStoreCoordinator)
}

1 个答案:

答案 0 :(得分:0)

最简单的解决方案是删除当前数据库并允许MagicalRecord创建一个没有任何迁移的新数据库。请记住只能 ONCE

static func mr_setupCoreDataStack() {

    var storePath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: SharedGroupName)
    storePath = storePath!.appendingPathComponent("POSowner.sqlite")
    try! FileManager.default.removeItem(at: storePath!)

}