I am using realm in my current application and I thought of an updating my app. Now, this process takes migrating of tables
and new entities and schema if you are using database in the app to save some values.
My issue here is that I am having some issues with migration because theres is not good documentation for Realm Migration yet and I am getting couple of errors which includes the error : Column type not valid
.
This is my approach towards migrating :
First, this is how the realm configuration looks like :
public class RealmHelper implements RealmMigration {
public static final long SCHEMA_VERSION = 2; // This was the 2nd schema.
public static final String REALM_NAME = "john.example";
public static RealmConfiguration getRealmConfig(Context context) {
return new RealmConfiguration.Builder(context)
.name(REALM_NAME)
.schemaVersion(SCHEMA_VERSION)
.migration(new Migration())
.build();
}
}
Second, this is the Migration class : This is where the issue is.
public class Migration implements RealmMigration {
@Override
public long execute(Realm realm, long version) {
if(version == 2){
// Issue is here. Notice the "otherModel". That is an entity in the SampleClass table.
Table sampleTable = realm.getTable(SampleClass.class);
sampleTable.addColumn(ColumnType.TABLE, "otherModel", true);
}
}
}
Last, the SampleClass, which is a wrapper for the actual Data model.
public class SampleClass extends RealmObject {
@SerializedName("somename")
private OtherModel otherModel;
public OtherModel getOtherModel() {
return otherModel;
}
public void setOtherModel(OtherModel otherModel) {
this.otherModel = otherModel;
}
}
Based on the current scenario, I am getting an error here where it says that the ColumnType is not valid.
Table sampleTable = realm.getTable(SampleClass.class);
sampleTable.addColumn(ColumnType.TABLE, "otherModel", true);
I am not sure what exactly can be the column type if its just an Object in the Wrapper Model.
I will really appreciate any kind of help here.. thanks in advance :)
If you want to add a reference to another RealmObject, it is called a Link:
sampleTable.addColumn(ColumnType.LINK, "otherModel", realm.getTable(OtherModel.class);
You can also see an example of it here: https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/model/Migration.java#L89-L89 except this references a RealmList instead of a RealmObject