Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm cannot migrate

I have a Realm model that I want to apply migrations. However, when I apply the migrations I get the error

Configurations cannot be different if used to open the same file. 
The most likely cause is that equals() and hashCode() are not overridden in the migration class: 

In my Activity class, the configuration is set as:

realmConfiguration = new RealmConfiguration
                .Builder(this)
                .schemaVersion(0)
                .migration(new Migration())
                .build();

I use the realm instance to get some values. And then I apply the migration using:

RealmConfiguration config = new RealmConfiguration.Builder(this)
            .schemaVersion(1) // Must be bumped when the schema changes
            .migration(new Migration()) // Migration to run
            .build();

Realm.setDefaultConfiguration(config);

When I call this: realm = Realm.getDefaultInstance(); I get the error above. Am I applying the migrations correctly?

like image 545
Wers1962 Avatar asked Jul 31 '16 14:07

Wers1962


2 Answers

Your migration should look like this:

public class MyMigration implements Migration {
    //... migration

    public int hashCode() {
       return MyMigration.class.hashCode();
    }

    public boolean equals(Object object) {
       if(object == null) { 
           return false; 
       }
       return object instanceof MyMigration;
    }
}
like image 80
EpicPandaForce Avatar answered Nov 17 '22 22:11

EpicPandaForce


Have you tried overriding equals and hashcode in your Migration class as the exception message says?

The most likely cause is that equals() and hashCode() are not overridden in the migration class
like image 27
Christian Melchior Avatar answered Nov 17 '22 23:11

Christian Melchior