【问题标题】:RealmMigrationNeededException when adding new schema with addRealmObjectField使用 addRealmObjectField 添加新架构时出现 RealmMigrationNeededException
【发布时间】:2019-02-07 13:11:48
【问题描述】:

我想将一个新架构添加到我的数据库中,并引用另一个新架构。

以下是模型:

open class Code(
    var name: String? = null,
    var code: String? = null
) : RealmObject()

open class Foo(
    var codes: RealmList<Code> = RealmList()
) : RealmObject()

还有迁移:

val codeSchema = schema.create("Code")
        .addField("name", String::class.java)
        .addField("code", String::class.java)

schema.create("Foo")
    .addRealmObjectField("codes", codeSchema)

但这会崩溃并出现以下错误:

io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
    - Property 'Foo.codes' has been changed from '<Code>' to 'array<Code>'.

因为这两个都是新的模型,我不知道为什么它告诉我某些东西“已经改变了”。

如何正确添加这两个模型?

【问题讨论】:

    标签: android kotlin realm realm-migration


    【解决方案1】:

    知道了。我需要使用addRealmListField() 而不是addRealmObjectField(),因为它引用的是一个列表而不是单个对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多