realm / realm-java

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

duplicate values after migration #6550

Closed saeedmozaffari closed 3 years ago

saeedmozaffari commented 5 years ago

Hi, I have following error for migration but this error is completely unusual, because never i didn't have any code for add "RealmRoomMessage.messageId" to the migration. But i have many errors like this at fabric and unfortunately can't reproduce this state for make error.

Our project is open source and this is "Migration Class"

Unable to create application net.iGap.G: java.lang.IllegalStateException: 
Primary key property 'RealmRoomMessage.messageId' has duplicate values after migration.

Realm version(s): v 5.9.0 Realm Sync feature enabled: No Android Studio version: 3.4 Android Build Tools version: 27.0.3 Gradle version: 3.1.4 Which Android version and device(s): just not happen in android version 7

Dunedan commented 5 years ago

We have at least one user facing the same problem: The same exception during migration even though we don't touch the affected schema. The primary key is present since the schema got added initially. We're also unable to reproduce this error.

java.lang.IllegalStateException:
    Primary key property 'Event.id' has duplicate values after migration.

Realm version(s): v5.11.0 Realm Sync feature enabled: No Android Studio version: 3.4 Android Build Tools version: 28.0.3 Gradle version: 5.4 Which Android version and device(s): Android 6.0.1 on Samsung Galaxy S7 Edge

Dunedan commented 5 years ago

Apparently this isn't the first time this issue got reported (see #5983 and #6221).

Dunedan commented 5 years ago

realm-cocoa is affected as well: https://github.com/realm/realm-cocoa/issues/5855

cmelchior commented 5 years ago

Hmm, yes looking at the code, this is your model class, right? https://github.com/KianIranian-STDG/iGap-Android/blob/master/app/src/main/java/net/iGap/realm/RealmRoomMessage.java#L70

And the only modification you have for the RealmRoomMessage is this one, correct? https://github.com/KianIranian-STDG/iGap-Android/blob/master/app/src/main/java/net/iGap/realm/RealmMigration.java#L35

This could indicate a bug on our end somewhere, but it is a bit unclear where.

ArtemKleinschmidt commented 5 years ago

Same issue, we have a lot of crashes on production, but we can not reproduce the problem. Schema that crashes was added two years ago, and was not changed since then.

Caused by java.lang.IllegalStateException: Primary key property 'MyClassName.id' has duplicate values after migration. at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(OsSharedRealm.java) at io.realm.internal.OsSharedRealm.<init> + 49(OsSharedRealm.java:49) at io.realm.internal.OsSharedRealm.getInstance + 13(OsSharedRealm.java:13) at io.realm.BaseRealm.<init> + 88(BaseRealm.java:88) at io.realm.BaseRealm.<init> + 4(BaseRealm.java:4) at io.realm.Realm.<init> + 12(Realm.java:12)

Realm version(s): v5.11.0 Realm Sync feature enabled: No Android Studio version: 3.4.2 Android Build Tools version: 28.0.3 Gradle version: 5.1.1 Which Android version and device(s): Android 6-9, a lot of crashes on Samsung S7 with android 8

mikim commented 5 years ago

any workaround? @cmelchior

        RealmObjectSchema gameInfo = schema.get("GameInfo");
        gameInfo.removeField("id");
        gameInfo.addField("id",int.class,FieldAttribute.PRIMARY_KEY);

at addField, it crashes with the same exception message:

Field 'id' cannot be set as primary key since there are duplicated values for field 'id' in Class 'GameInfo'

Zhuinden commented 5 years ago

Oh that's actually quite simple. If you remove the field, then add the field, the default values will be 0 for every single int field, and therefore it will violate the primary key constraint.

You need to create a new field (without PK constraint), transform the existing values to that field, delete the original field, rename the field to the original name, then add the primary key constraint.

cmelchior commented 3 years ago

I'll assume the comment from Zhuinden described the underlying problem. So I'll close this issue. If it is still relevant we probably need something that reproduces it in order to investigate.