realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.45k stars 1.75k forks source link

Used setNulllable/setNull and I'm getting Property has been made optional crash #7690

Closed davieskent closed 1 year ago

davieskent commented 2 years ago

How frequently does the bug occur?

Sometimes

Description

I've updated my app on the play store and started having the crash:

Fatal Exception: java.lang.IllegalArgumentException: Illegal Argument: This field(name) is not nullable.
       at io.realm.internal.UncheckedRow.nativeSetNull()
       at io.realm.internal.UncheckedRow.setNull(:286)
       at io.realm.internal.CheckedRow.setNull(:96)
       at io.realm.DynamicRealmObject.setNull(:1051)
       at 

I've changed my model from:

@Required
@RealmField(NAME)
var name: String = ""

to:

@Required
@RealmField(NAME)
var name: String? = null

When the model was created, it was like addField(PersonRealm.NAME, String::class.java, isRequired = true)

And on the migration, only have added:

schema.get(PersonRealm::class.java.simpleName)?.apply {
    transform { dynamicRealmObject ->
        dynamicRealmObject.setNull(PersonRealm.NAME)
    }
}

I've removed the @Required annotation from the field and changed my migration to:

schema.get(PersonRealm::class.java.simpleName)?.apply {
    setNullable(PersonRealm.NAME, true)
    transform { dynamicRealmObject ->
        dynamicRealmObject.setNull(PersonRealm.NAME)
    }
}

I've locally tested, and it seemed the migration was now correct, but after uploading a new version to the store I'm still having the crash (way less crashes nevertheless):

Fatal Exception: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'PersonRealm.name' has been made optional.
       at io.realm.internal.OsSharedRealm.nativeGetSharedRealm()
       at io.realm.internal.OsSharedRealm.<init>(:173)
       at io.realm.internal.OsSharedRealm.getInstance(:249)

Do I need to change anything more in my migration?

Stacktrace & log output

No response

Can you reproduce the bug?

Not yet

Reproduction Steps

No response

Version

10.4.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

Yes, using encryption

Platform OS and version(s)

Android 9 and 11

Build environment

No response

sync-by-unito[bot] commented 1 year ago

➤ Clemente Tort Barbero commented:

I think some of the devices did not migrate during the first update as no migration was provided, and now they are unable to update because they are on a different db version.

I would recommend bumping the version and then doing a migration step to address these issues.

rorbech commented 1 year ago

@davieskent Is this still a problem or do you have any further observations that could shed light on the issue?

github-actions[bot] commented 1 year ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.