【问题标题】:Realm - Column Type is not vaid领域 - 列类型无效
【发布时间】:2015-11-13 21:41:47
【问题描述】:

我在我当前的应用程序中使用领域,我想更新我的应用程序。现在,如果您在应用程序中使用数据库来保存一些值,则此过程需要 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();
    }
}

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

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

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

在这里我将非常感谢任何形式的帮助.. 在此先感谢 :)

【问题讨论】:

    标签: android realm realm-migration


    【解决方案1】:

    如果要添加对另一个 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

    【讨论】:

    • 明白了..感谢您的解释..真的很有帮助。是的,ColumnType.LINK 是我在文档中找不到的。谢谢你.. :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多