I’m having trouble updating a Realm object in React Native Realm. I’m querying for the object, able to console.log the object, but once I try to update the object by setting one of its keys, I get this error:
Attempting to create an object of type ‘[OBJECT]’ with an existing primary key value ‘5f76395eeca29f5aeb466873’
I’m particularly confused because I’m able to successfully update an object using that pattern (querying then mutating) in a different component.
@Jerry_Wang You cannot change primary keys once set - if you want to do that you should delete the object and then recreate a new one with the primary key you want.
@Jerry_Wang I believe you either need to use the upsert method with “modified” parameter passed -
Or realmObject in your code snippet is actually a realm result and you need to get the actual indice of the object you are trying to update, as in - realmObject[0]
It looks like upserting gives me the same error. Are there logs I can find beyond the ones provided by the Realm console that would help me debug what’s going on further?
realmObject.updatedAt = "Fri, 25 Sep 2020 02:39:28 GMT" will not work since updateAt isn’t a string. realmObject.updatedAt = new Date("Fri, 25 Sep 2020 02:39:28 GMT") is better.