Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-1533

Intentional change to `realm.create()` when `UpdateMode` not specified?

      Description

      I'm testing the pre-release branch with our existing app. We had some code creating a bunch of nested items with UUID IDs for the primary keys in a loop which now throws an error but worked in 10.20.0 and earlier.

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      function loadThings(importModel: ImportModel[]) {
          realm.write(() => {
              for (const item of importModel) {
      
                  const savedRealmThing = realm.create<RealmThing>(RealmThing.schema.name, createRealmThing(item))
      
                  const aliases = item.aliases ? item.aliases : []
                  const thingNames = [...aliases, item.name]
                  thingNames.forEach((name) => {
                      const alias = {
                          id: uuid.v4(),
                          thing: savedRealmThing,
                          name: name,
                          schema: latestRealmSchema.realmAlias,
                      }
                      realm.create<RealmAlias>(RealmAlias.schema.name, alias)
                  })
              }
          })
          return importModel.length
      }
      

      When it gets to realm.create<RealmAlias>(RealmAlias.schema.name, alias) it dies, apparently because it's trying to recreate the nested savedRealmThing and the object already exists. Changing that line to realm.create<RealmAlias>(RealmAlias.schema.name, alias, Realm.UpdateMode.Modified) fixes the issue.

      It seems like the existing behaviour for realm.create() with no UpdateMode was Modified but is now Never, was this a deliberate change?

      Stacktrace & log output

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      Error is: `Exception in HostFunction: Attempting to create an object of type 'RealmThing' with an existing primary key value ''0c5f1ca1-1e08-48a2-a75b-e5a302db67d5''.`
      

      Can you reproduce a bug?

      Yes, always

      Reproduction Steps

      See example code in description

      Version

      hermes (10.20.0-beta.1)

      What SDK flavour are you using?

      Local Database only

      Are you using encryption?

      No, not using encryption

      Platform OS and version(s)

      RN 0.66.3 using JSC (not Hermes!) - iPhone 13 Simulator w/ iOS 15.2, Android AVD Emulator running Android 9.0

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: