Uploaded image for project: 'Realm .NET SDK'
  1. Realm .NET SDK
  2. RNET-1143

Error when trying to migrate IList data type with DynamicApi.GetList

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

      What happened?

      I'm migrating data that was previously "float?" and IList<float?" to the type "string" and "IList<string>". I am attaching the code that performs this migration, and everything goes well when I give a DynamicApi.GetList in an IEmbeddedObject, however, when I call a DynamicApi.GetList in a "float?" gives the error "KeyNotFoundException: The given key '' was not present in the dictionary". him being the "float?" type. The error only happens when I try to call a GetList in the "MeasuredValues" field, the rest of the code works fine and performs the correct conversion. Does anyone have an idea what I might be doing wrong? See the error that appears:

      See how MeasuredValues ​​is in the database, it appears in the scope as List<float> but it is a List<float?>, I believe this is a bug in the realm, but I guarantee that in the code we are saving it as IList<float?>.

      I'm attaching a drive with the old bank (the bank that tries to get "MeasuredValues") in case it helps with anything. Link drive :
      LinkDrive

      Repro steps

      1. Save an object in the database with the field type "IList<float?>
      2. Perform a database migration by calling GetList from an "Ilist<float?">

      The error occurs exactly on this line:

      Version

      .NET Framework

      What Atlas Services are you using?

      Local Database only

      What type of application is this?

      Unity

      Client OS and version

      Unity

      Code snippets

      if (oldSchemaVersion < 2)
      {
          var oldOrders = migration.OldRealm.DynamicApi.All("OrderServiceEntity");
          var newOrders = migration.NewRealm.All<OrderServiceEntity>();
      
          for (int i = 0; i < newOrders.Count(); i++)
          {
              var oldOrder = oldOrders.ElementAt(i);
              var newOrder = newOrders.ElementAt(i);
      
              var oldCalibration = oldOrder.DynamicApi.Get<IEmbeddedObject>("Calibration");
              var newCalibration = newOrder.Calibration;
      
              if (oldCalibration != null)
              {
                  for (int j = 0; j < oldCalibration.DynamicApi.GetList<IEmbeddedObject>("TablePages").Count; j++)
                  {
                      IEmbeddedObject oldPage = oldCalibration.DynamicApi.GetList<IEmbeddedObject>("TablePages")[j];
                      var newPage = newCalibration.TablePages[j];
      
                      for (int k = 0; k < oldPage.DynamicApi.GetList<IEmbeddedObject>("Rows").Count; k++)
                      {
                          UnityEngine.Debug.LogError(k);
                          var oldRow = oldPage.DynamicApi.GetList<IEmbeddedObject>("Rows")[k];
                          var newRow = newPage.Rows[k];
      
                          // Convertendo o campo Nominal
                          var oldNominal = oldRow.DynamicApi.Get<float?>("Nominal");
                          newRow.Nominal = oldNominal.HasValue ? oldNominal.Value.ToString() : null;
      
                          // The error is here on this line:
                          IList<float?> oldMeasuredValues = oldRow.DynamicApi.GetList<float?>("MeasuredValues");
      
                          var newMeasuredValues = new List<string?>();
                          foreach (var value in oldMeasuredValues)
                          {
                              newMeasuredValues.Add(value.HasValue ? value.Value.ToString() : null);
                          }
      
                          newRow.MeasuredValues.Clear();
                          foreach (var newMeasured in newMeasuredValues)
                          {
                              newRow.MeasuredValues.Add(newMeasured);
                          }
                      }
                  }
              }
          }
      }
      

      Stacktrace of the exception/crash you're getting

      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
      KeyNotFoundException: The given key '' was not present in the dictionary.
      System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <51ee0c51c90047b488b10b1b78b365d8>:0)
      Realms.Realm+RealmMetadata.get_Item (System.String objectType) (at <20c8551e199242abadc6d7b62871711c>:0)
      Realms.ObjectHandle.GetList[T] (Realms.Realm realm, System.String propertyName, Realms.Metadata metadata, System.String objectType) (at <20c8551e199242abadc6d7b62871711c>:0)
      Realms.DynamicObjectApi.GetList[T] (System.String propertyName) (at <20c8551e199242abadc6d7b62871711c>:0)
      Ark.API.CMMS.Database.CmmsDatabase.MigrationCallBack (Realms.Migration migration, System.UInt64 oldSchemaVersion) (at Assets/Scripts/Api/CMMS/Database/CmmsDatabase.cs:174)
      Realms.SharedRealmHandle.OnMigration (System.IntPtr oldRealmPtr, System.IntPtr newRealmPtr, System.IntPtr migrationSchema, Realms.Native.Schema oldSchema, System.UInt64 schemaVersion, System.IntPtr managedConfigHandle) (at <20c8551e199242abadc6d7b62871711c>:0)
      Rethrow as AggregateException: Exception occurred in a Realm.MigrationCallback callback. (The given key '' was not present in the dictionary.)
      Realms.NativeException.ThrowIfNecessary () (at <20c8551e199242abadc6d7b62871711c>:0)
      Realms.SharedRealmHandle.Open (Realms.Native.Configuration configuration) (at <20c8551e199242abadc6d7b62871711c>:0)
      Realms.RealmConfiguration.CreateHandle (Realms.Native.Configuration& configuration) (at <20c8551e199242abadc6d7b62871711c>:0)
      Realms.RealmConfiguration+<>c__DisplayClass29_0.<CreateHandleAsync>b__1 (System.Threading.Tasks.Task _) (at <20c8551e199242abadc6d7b62871711c>:0)
      System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () (at <51ee0c51c90047b488b10b1b78b365d8>:0)
      System.Threading.Tasks.Task.Execute () (at <51ee0c51c90047b488b10b1b78b365d8>:0)
      --- End of stack trace from previous location where exception was thrown ---
      Realms.RealmConfigurationBase.CreateRealmAsync (System.Threading.CancellationToken cancellationToken) (at <20c8551e199242abadc6d7b62871711c>:0)
      Ark.Persistence.Database.RealmDatabase.InitAsync () (at
      

      Relevant log output

      No response

            Assignee:
            nikola.irinchev@mongodb.com Nikola Irinchev
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: