我在我当前的应用程序中使用领域,我想到了更新我的应用程序。现在,如果您在应用程序中使用数据库来保存某些值,则此过程将使用migrating of tables
和新实体和架构。
我的问题是我在迁移方面遇到了一些问题,因为对于Realm Migration来说,这还不是很好的文档,我收到的错误包括error : Column type not valid
。
这是我的迁移方法:
首先,这是领域配置的样子:
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();
}
}
其次,这是Migration类:这就是问题所在。
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);
}
}
}
最后,SampleClass,它是实际数据模型的包装器。
public class SampleClass extends RealmObject {
@SerializedName("somename")
private OtherModel otherModel;
public OtherModel getOtherModel() {
return otherModel;
}
public void setOtherModel(OtherModel otherModel) {
this.otherModel = otherModel;
}
}
根据当前场景,我在这里收到一个错误,其中表示ColumnType无效。
Table sampleTable = realm.getTable(SampleClass.class);
sampleTable.addColumn(ColumnType.TABLE, "otherModel", true);
我不确定列类型究竟是什么,如果它只是包装器模型中的一个对象。
我真的很感激这里的任何帮助..在此先感谢:)
答案 0 :(得分:1)
如果要添加对另一个RealmObject的引用,则称为链接:
sampleTable.addColumn(ColumnType.LINK, "otherModel", realm.getTable(OtherModel.class);
你也可以在这里看到它的一个例子:https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/model/Migration.java#L89-L89,除了它引用一个RealmList而不是一个RealmObject