Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-1252

Impossible to delete objects of removed type during Migration

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • Q2-22FY
    • Affects Version/s: None
    • Component/s: None

      Goals

      Let's say I had objects of type Dog and Person in my Realm database, where Person has a property of List<Dog>. In my update, I want to remove completely the dogs. So I do the following:

      • I remove the property of type List<Dog> from Person
      • I remove Dog.self from configuration.objectTypes to that the type Dog is no longer used in this Realm. In my case, I keep the Dog class because I need it for another Realm.
      • I increment the schema version number
      • I add a migration block that deletes all the objects of type Dog

      Expected Results

      I expect that the migration block completes without error and that after, there is no more object of type Dog in my database. In fact, if my database only contained Person and Dog objects, and I removed all the Person objects for some reason, I expect that realm.isEmpty is true.

      Actual Results

      If I do migration.deleteData(forType: "Dog") in the migration, I get the error Table is target of cross-table link columns. Note that Dog was referenced by Person in the old schema, but is not referenced by any other class in the new schema.

      If I do:

      migration.enumerateObjects(ofType: "Dog") { (oldObject, newObject) in
           migration.delete(oldObject!)
      }
      

      I get the error Can only delete an object from the Realm it belongs to. See #6799.

      If I do:

      migration.enumerateObjects(ofType: "Dog") { (oldObject, newObject) in
           migration.delete(newObject!)
      }
      

      It crashes because newObject is nil for all the objects of type Dog.

      Steps for others to Reproduce

      Have a first schema version with types Dog and Person where Person has a List<Dog>. Now remove the List<Dog> property of Person, and remove Dog.self from configuration.objectTypes. In your migration block, try to delete the objects of type Dog.

      Code Sample

      See above.

      Version of Realm and Tooling

      Realm framework version: 10.7.2
      Xcode version: 12.4
      iOS/OSX version: 14.4.2
      Dependency manager + version: CocoaPods

            Assignee:
            diana.perez@mongodb.com Diana Maria Perez Afanador (Inactive)
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: