迁移后主键属性具有重复值 - 域迁移

时间:2016-02-26 08:08:17

标签: swift realm realm-migration

我有一个包含两个模型的现有Realm项目

Model1 -> id, updateDate, details
Model2 -> id, updateDate, title, model1

(是的,我使用了一个类对象而不是id - aargh。两个id都是主键)

使用我的应用的更新版本,我要向Model1 title添加新属性,并将Model2.model1从类型Model1更改为string类型}(= Model1.id

我根据提供的示例为此编写了一个迁移块

let migrationBlock: (RLMMigration, UInt64) -> Void = { (migration, oldSchemeVersion) in
    if oldSchemeVersion < 1 {
        migration.enumerateObjects(Model1.className()) { oldObject, newObject in
            //Nothing needed, the title can be a blank
        }
        migration.enumerateObjects(Model2.className()) { oldObject, newObject in
            if let oldModel1 = oldObject!["model1"] as? RLMDynamicObject {
                newObject!["model1"] = oldModel1["id"]
            }
        }
    }
}

let config = RLMRealmConfiguration.defaultConfiguration()
config.schemaVersion = newSchemaVersion
config.migrationBlock = migrationBlock
RLMRealmConfiguration.setDefaultConfiguration(config)

但是在迁移结束时,当我尝试访问默认域(Realm.defaultRealm)时,它会因此错误而失败

Terminating app due to uncaught exception 'RLMException', reason: 'Primary key property 'id' has duplicate values after migration.'

我无法弄清楚为什么会出现这种问题以及我应该做些什么来使这项工作成功。任何帮助将不胜感激。

注意 - 我的代码使用Realm objective-c代码,但在Swift应用程序中

0 个答案:

没有答案
相关问题