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

[Migration] Cannot populate new lists during Realm migration? ( Objects get created but list remains empty? )

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      Bug:

      After modifying schema to add new list to an old object, I can NOT populate the new list during Realm migration?

      • When new list is a list of objects, the new objects that are pushed get created as expected.
      • However, the resulting new list is always empty when viewed in Realm Browser?

      Goals

      To create a new list of objects and populate them during the Realm migration.

      Expected Results

      I expect the new objects to be created and contained in the new object list.

      Actual Results

      Pushing the object on the list creates the object, yet list remains empty?

      I tried both object lists and arrays of types. I can add items to a string array list, or I can add objects to an object list, yet lists always remain empty.

      Steps to Reproduce

      1) Add a new list to existing object.

      • Type does not seem to matter.
      • If type is an object, then pushing object onto the list will create object in database, but dabase list is always empty.
        2) Updated schema version to force migration
        3) During migration push new items onto the new string list and object list
        4) I can console.log the list length, and see length increasing during migration, but list in database is always empty?

      Code Sample

      PointContainer.schema = {
        name: 'PointContainer',
        schemaVersion,
        properties: {
          index: { type: 'int', default: -1 },
        },
      };
      
      class MyObject {
      }
      MyObject.schema = {
        name: 'MyObject',
        schemaVersion,
        properties: {
          uuidParent: { type: 'string' },
      
          // These are the two new lists added to schema
          // Goal: To populate lists during Realm migration
          newStringList: 'string[]',
          newPointContainerList: 'PointContainer[]',
        },
      };
      
      export default new Realm({
        path: Platform.OS === 'android' ? 'database.realm' : 'db/database.realm',
        schema: [
          MyObject,
          PointContainer,
        ],
        schemaVersion,
        migration(oldRealm, newRealm) {
      
          if (oldRealm.schemaVersion < SchemaVersionAddNewList) {
            try {
              const myObjectListOld = oldRealm.objects('MyObject');
              const myObjectListNew = newRealm.objects('MyObject');
              for (let i = 0; i < myObjectListOld.length; i += 1) {
                const myObjectOld = myObjectListOld[i];
                const myObjectNew = myObjectListNew[i];
      
                // This result is an empty myObjectNew.newStringList
                myObjectNew.newStringList.push('one');
                myObjectNew.newStringList.push('two');
                myObjectNew.newStringList.push('three');
      
                // New PointContainer objects will be created
                // This result is an empty myObjectNew.newPointContainerList
                myObjectNew.newPointContainerList.push({ index: 1});
                myObjectNew.newPointContainerList.push({ index: 2});
                myObjectNew.newPointContainerList.push({ index: 3});
              } //
            } catch (error) {
             // No errors are displayed or caught?
              console.log('dbg: *** Error migration:\n', error);
            }
          }
        },
      });
      
      

      Version of Realm and Tooling

      • "realm": "^2.15.3",
      • "react": "16.3.1",
      • "react-native": "0.55.4",
      • iOS and Android both
      • Which debugger for React Native: None

      Thanks in advance,
      -Ed

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

              Created:
              Updated: