7

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?

Wers1962
  • 71
  • 2

5 Answers5

8

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;
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • `instanceof` implies non-nullity so I think the `equals` method can be simplified by removing the null check altogether. – Nikos Hidalgo Nov 26 '19 at 16:29
1

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
Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
0

Add the schema version as field in MyMigration, and Override the equals():

    private final int version;

    @Override
    public boolean equals(Object o) {
        return this.version == ((MyMigration)o).version;
    }

    public MyMigration(int version) {
        this.version = version;
    }
herbertD
  • 10,657
  • 13
  • 50
  • 77
  • This had negative votes when we checked it out, but it legit. The thing is, realm tries verifying that the migrations are the same for two configurations, so if you are not using singleton configurations, just matching on class types will do the trick. – Andreas Rudolph May 12 '22 at 10:59
0

Override the equals and hashcode methods in your Migration class as shown below:

@Override
public boolean equals(Object obj) {
    return obj != null && obj instanceof Migration; // obj instance of your Migration class name, here My class is Migration.
}

@Override
public int hashCode() {
    return Migration.class.hashCode();
}
0

I believe the issue is the first part of the message "Configurations cannot be different if used to open the same file." You are using two different configurations to open realm. One of your examples uses schemaVersion 0 and the other uses schemaVersion 1. You should be using the same version throughout your app.

Every time you need a new data migration bump up the schema version number, and add code in the class Migration that looks at the old/new schema versions and does appropriate migrations.

dweebo
  • 3,222
  • 1
  • 17
  • 19