Uploaded image for project: 'Realm Core'
  1. Realm Core
  2. RCORE-773

Throw an error when updating primary key field

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None

      Expected results

      I don't think it should be possible to update a primary key field of an object. So I would expect the C-API to throw an error if doing so. If it should be possible to update it, I would at least expect a check that you are not creating duplicate primary keys when setting it.

      Actual Results

      In realm-kotlin I am able to define a schema with a primary key and after creating of an instance I am able to update the primary key field even setting it to the same primary key value of an already existing object.

      I#### Steps & Code to Reproduce

      class PrimaryKeyString : RealmObject {
          @PrimaryKey
          var primaryKey: String = Random.nextULong().toString()
      }
      
      realm.writeBlocking {
          val first = copyToRealm(PrimaryKeyString().apply { primaryKey = PRIMARY_KEY })
          val second = copyToRealm(PrimaryKeyString().apply { primaryKey = "Other key" })
          
          // expect update to cause an error
          assertFailsWith<RuntimeException> { 
              second.primaryKey = PRIMARY_KEY
          }
          
          // eliminating the above assert will render the following to pass, which shows that we end up with two objects both with the same primary key value
          val objects = objects(PrimaryKeyString::class)
          assertEquals(2, objects.size)
          val first2 = objects[0]
          val second2 = objects[1]
          assertEquals(objects[0].primaryKey, objects[1].primaryKey)
      }
      

      Core version

      Core version: 10.8.0

            Assignee:
            Unassigned Unassigned
            Reporter:
            claus.rorbech@mongodb.com Claus Rørbech (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: