4

I have an existing Realm project with two models

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

(yes I used a class object instead of the id - aargh. Both id are primary keys)

With an updated version of my app, I am adding a new property to Model1 (title) and changing Model2.model1 from type Model1 to type string (=Model1.id)

I wrote a migration block for this as per the samples provided

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)

But at the end of the migration, when I try to access the default realm (Realm.defaultRealm), it fails with this error

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

I cannot figure out why this is going wrong and what I am supposed to do to make this work. Any help would be appreciated.

NOTE - my code uses the Realm objective-c code but in a Swift app

lostInTransit
  • 70,519
  • 61
  • 198
  • 274
  • What do you mean by- "(yes I used a class object instead of the id - aargh. Both id are primary keys) "? – Shripada Feb 26 '16 at 08:46
  • I should have stored the model1.id in model2, but instead I stored a reference to model1 object. I wish to change that with the migration and store model1.id in model2 – lostInTransit Feb 26 '16 at 08:48
  • I am afraid, you can't do this!, changing data type of a property is something not possible IMO. – Shripada Feb 26 '16 at 08:50
  • Just tried the same thing after changing the name of the property in the new schema. Same error. So thats not the issue – lostInTransit Feb 26 '16 at 08:55
  • Hmm, try assigning the newObject!["id"] also. I am not seeing that happening. – Shripada Feb 26 '16 at 10:09
  • Just did. I assigned all properties along with the new one, same error. Even tried assigning a random UUID to each newObject, no joy – lostInTransit Feb 26 '16 at 10:55
  • I've attempted to create a repro case for your issue, but was unable to trigger the exception you've mentioned. See https://gist.github.com/jpsim/b6bc0b8b3f13282ca44f – jpsim Mar 09 '16 at 23:00
  • 1
    Got a response from another Realm developer. With the realm update, the concept of required vs optional properties came in. So had to add relevant migration code for that. Amazing support from the team @jpsim – lostInTransit Mar 10 '16 at 05:00
  • 1
    @lostInTransit you should include what your solution was after Realm support helped you and select that as the solution of the question – Jan Jul 11 '19 at 17:31

0 Answers0