[英]How to migration in realm for new table in android?
I add a new table to my realm database.我向我的领域数据库添加了一个新表。 For migration, I try at this way:对于迁移,我尝试这样:
realm.schema.create("AddTodoModel")
.addField("id", Long::class.java, FieldAttribute.PRIMARY_KEY)
.addField("title", String::class.java , FieldAttribute.INDEXED)
.addField("list", RealmList::class.java , FieldAttribute.INDEXED)
.addField("color", Int::class.java , FieldAttribute.INDEXED)
And this is AddTodoModel class:这是 AddTodoModel 类:
open class AddTodoModel(
@PrimaryKey
var id: Long,
var title: String,
var list: RealmList<TodoModel>,
var color: Int
) : RealmObject() {
constructor() : this(0, "", RealmList<TodoModel>(), R.color.black)
}
And when start the app, I get this error:当启动应用程序时,我收到此错误:
java.lang.IllegalArgumentException: Use addRealmObjectField() instead to add fields that link to other RealmObjects: list
I just get this error when the app updated.我只是在应用程序更新时收到此错误。 So, How to fix this error and migrate to a new version?那么,如何修复此错误并迁移到新版本?
After trying some way, I found the solution.尝试了一些方法后,我找到了解决方案。 Maybe someone needs this: I create a list as TodoModel class in the database, But I miss to create a migration for that.也许有人需要这个:我在数据库中创建了一个列表作为 TodoModel 类,但我想为此创建一个迁移。 And also I need to use FieldAttribute.REQUIRED
instead of FieldAttribute.INDEXED
.而且我还需要使用FieldAttribute.REQUIRED
而不是FieldAttribute.INDEXED
。 So this is the final migration code:所以这是最终的迁移代码:
realm.schema.create("AddTodoModel")
.addField("id", Long::class.java, FieldAttribute.PRIMARY_KEY)
.addField("title", String::class.java, FieldAttribute.REQUIRED)
.addRealmListField(
"list",
realm.schema.create("TodoModel")
.addField("isDone", Boolean::class.java, FieldAttribute.REQUIRED)
)
.addField("color", Int::class.java, FieldAttribute.REQUIRED)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.