hardysim
hardysim

Reputation: 2866

RealmMigrationNeededException when adding new schema with addRealmObjectField

I want to add a new schema to my DB referencing another new schema.

Here are the models:

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

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

And the migration:

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

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

But this crashes with the following error:

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

Because these are both new models, I don't know why it tells me that something "has been changed".

How can I add those two models correctly?

Upvotes: 2

Views: 499

Answers (1)

hardysim
hardysim

Reputation: 2866

Got it. I need to use addRealmListField() instead of addRealmObjectField() because it references a list and not a single object.

Upvotes: 3

Related Questions