领域 - 列类型无效

Realm - Column Type is not vaid

我在我当前的应用程序中使用领域,我想更新我的应用程序。现在,如果您在应用程序中使用数据库来保存一些值,则此过程需要 migrating of tables 和新的实体和模式。

我的问题是我在迁移方面遇到了一些问题,因为目前还没有很好的领域迁移文档,而且我遇到了一些错误,其中包括 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();
    }
}

其次,这是迁移class:这就是问题所在。

    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); 

我不确定列类型到底是什么,如果它只是包装模型中的一个对象。

非常感谢您提供的任何帮助.. 提前致谢:)

如果你想添加对另一个RealmObject的引用,它被称为Link:

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