RealmMigrationNeededException even with migration


Hi, i am getting this exception for some user, i am sure i am setting the migration class to default realm config and also i added the fields to the model classes , but for some reason i am getting this error when i try to access to a realm instance
Oncreate in application class

        RealmConfiguration config =
                new RealmConfiguration.Builder()
                        .migration(new TwnelMigration())
public class TwnelMigration implements RealmMigration {
    public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
        RealmSchema realmSchema = realm.getSchema();
        // Migrate from version 1 to version 2
        if (oldVersion == 1) {
            RealmObjectSchema chatSchema = realmSchema.get("Chat");
                    .addField("typing", boolean.class)
                    .addField("typingTime", long.class);
        else if (oldVersion == 2) {
            RealmObjectSchema chatSchema = realmSchema.get("Chat");
            RealmObjectSchema contactSchema = realmSchema.get("Contact");
            // Create a new class
            RealmObjectSchema affiliationSchema = realmSchema.create("Affiliation")
                    .addRealmObjectField("contact", contactSchema)
                    .addField("affiliation", int.class)
                    .addField("room", String.class);

            chatSchema.addRealmListField("affiliations", affiliationSchema);


but in the documentation example for migration i see

if (oldVersion == 0) {
            .addField("name", String.class)
            .addField("age", int.class);

     // Migrate to version 2: Add a primary key + object references
     // Example:
     // public Person extends RealmObject {
     //     private String name;
     //     @PrimaryKey
     //     private int age;
     //     private Dog favoriteDog;
     //     private RealmList<Dog> dogs;
     //     // getters and setters left out for brevity
     // }
     if (oldVersion == 1) {
            .addField("id", long.class, FieldAttribute.PRIMARY_KEY)
            .addRealmObjectField("favoriteDog", schema.get("Dog"))
            .addRealmListField("dogs", schema.get("Dog"));

check the IF without the ELSE, the question is if it could be the reason for this exception ??


Specify the exception stack trace so that we know what’s missing. Realm-Java includes detailed exception messages for “migration needed exceptions” for what is different compared to what is expected.

Also, default schema version is 0, not sure if that is handled (0 -> 1).