3

Here is Code of my Realm Configuration. Issue is, When I remove or add any PrimaryKey I'm getting RealmMigrationNeededException exception.

previsoly UpgradeInfo.class, RealmApplication.class

public class UpgradeInfo extends RealmObject{

    @SerializedName("version")
    @PrimaryKey
    private int versionCode;

    @SerializedName("version_name")
    private String versionName;
}

public class RealmApplication extends MultiDexApplication {

    @Override 
    public void onCreate() {
        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .schemaVersion(1)
                .build();
        Realm.setDefaultConfiguration(config);
    }
}

the new UpgradeInfo.class,RealmApplication.class

public class UpgradeInfo extends RealmObject{

    @PrimaryKey
    private String packageName

    @SerializedName("version")
    private int versionCode;

    @SerializedName("version_name")
    private String versionName;
}

public class RealmApplication extends MultiDexApplication {

    @Override 
    public void onCreate() {
        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .schemaVersion(2)
                .migration(new Migration())
                .build();
        Realm.setDefaultConfiguration(config);
    }
}

public class ZNJRealmMigration implements RealmMigration {

    @Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
        RealmSchema schema = realm.getSchema();
        if (oldVersion == 1) {
                     schema.get("UpgradeInfo").removePrimaryKey().addField("packageName", String.class).addPrimaryKey("packageName");

        }
    }
}

Here I'm getting this error:

java.lang.RuntimeException: Unable to stop activity {com.example.realm/com.example.realm.main.ui.MainActivity}:
 io.realm.exceptions.RealmMigrationNeededException:
Migration is required due to the following errors:
- Primary Key for class 'UpgradeInfo' has been added.
        at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3668)
        at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3722)
        at android.app.ActivityThread.access$1100(ActivityThread.java:182)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1522)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5682)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Primary Key for class 'UpgradeInfo' has been added.
        at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
        at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:184)
        at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:254)
        at io.realm.BaseRealm.<init>(BaseRealm.java:124)
        at io.realm.BaseRealm.<init>(BaseRealm.java:93)
        at io.realm.Realm.<init>(Realm.java:153)
        at io.realm.Realm.createInstance(Realm.java:424)
        at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:342)
        at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282)
        at io.realm.Realm.getDefaultInstance(Realm.java:332)
        at com.example.realm.db.RealmHelper.<init>(RealmHelper.java:35)
        at com.example.realm.db.RealmHelper.getInstance(RealmHelper.java:45)
        at com.example.realm.util.Util.closeRealm(Util.java:181)
        at com.example.realm.main.ui.MainActivity.onStop(MainActivity.java:82)
        at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1288)
        at android.app.Activity.performStop(Activity.java:6328)
        at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3665)
        ... 10 more
martin
  • 31
  • 5

1 Answers1

0

When you create a RealmConfiguration without a schema version, its schema version is set to 0.

So your code most likely doesn't work because either:

  1. you are using a different configuration that does not have the migration set

  2. your schema version used to be 0 and not 1, so you aren't actually doing the schema operations that would change the primary key

Tim
  • 41,901
  • 18
  • 127
  • 145
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428