realm-auto-migration icon indicating copy to clipboard operation
realm-auto-migration copied to clipboard

"Table is target of cross-table link columns" exception when a table with reference is removed

Open sabid-habib opened this issue 6 years ago • 4 comments

I had two realm objects previously,

Cat extends realmObject{ String name; String color; }

Person extends realmObject{ String name; RealmList<Cat> cats; }

when I remove both tables "Cat" and "Person", the migrate() method on the Automigration class thwros the following exception on line: 107

 `Caused by: java.lang.IllegalStateException: Table is target of cross-table link columns in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsObjectStore.cpp line 140`

is there anything I am doing wrong here? If so, what's the proper way to remove the table with a reference?

sabid-habib avatar Aug 09 '18 04:08 sabid-habib

is there anything I am doing wrong here?

No, I am.

If so, what's the proper way to remove the table with a reference?

I should only remove the classes from the schema after the fields are matched.


Thanks for the report.

Zhuinden avatar Aug 09 '18 10:08 Zhuinden

hi @Zhuinden the similar exception is still occurring after your latest commit cbf0e73.

Caused by: java.lang.IllegalStateException: Table is target of cross-table link columns in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsObjectStore.cpp line 140

It's occurring as the schema is trying to remove the "Cat" table before removing the "cats" field from "Person" table or removing the "Person" table is removed itself(the removal is done alphabatically. 'Person' comes after 'Cat'). I guess, the matchField() method is not removing the "cats" field from "Person" table as "Person" table is not part of the current model classes (As "Person" table is also removed).

sabid-habib avatar Aug 12 '18 06:08 sabid-habib

removing the fields of the removed tables first is doing the job for now. on line: 95

if (modelClassNames.contains(objectClassName)) {
                // the model was found in the schema, we must match their fields.
                Class<? extends RealmModel> modelClass = modelClassNameToClassMap.get(objectClassName);
                matchFields(realmSchema, objectSchema, modelClass);
            } else {
                removeUnusedFields(objectSchema);
            } 
private void removeUnusedFields(RealmObjectSchema objectSchema) {
        Set<String> schemaFieldNames = objectSchema.getFieldNames(); // field names require `-keep public class * extends io.realm.RealmObject { *; }`
        for (String schemaFieldName : schemaFieldNames) {
            objectSchema.removeField(schemaFieldName);
        }
    }

although I think it's not a great way to do it. Please let me know if you think there is a better way to handle the scenario.

sabid-habib avatar Aug 12 '18 09:08 sabid-habib

Hrmm. This might even be a new change in Object-Store that I'm not aware of -_-

I think this solution should work. You don't really want to tinker with checking if the field is a link type or not and etc.

Zhuinden avatar Aug 12 '18 09:08 Zhuinden