androidrealmrealm-mobile-platformrealm-migration

RealmMigrationNeededException: Migration is required Realm, while adding new table on existing database, Android


I am using Realm gradle version "3.0.0", I have two table "Word" & "Favourite" like below

enter image description here

Now I want to add third Table named "History"

for that i have done below code

@Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
        final RealmSchema schema = realm.getSchema();

        if (oldVersion == 0) {
            RealmObjectSchema wordSchema = schema.get("Word");
            wordSchema.addField(DBHelper.COLUMN_NAME_PRONOUCE, String.class);
            wordSchema.addField(DBHelper.COLUMN_NAME_TYPE, String.class);
            oldVersion++;
        } if(oldVersion == 1){
            RealmObjectSchema favouriteSchema = schema.get("Favourite");
            favouriteSchema.addField(DBHelper.COLUMN_NAME_ID, String.class);
            favouriteSchema.addField(DBHelper.COLUMN_NAME_WORD, String.class);
            favouriteSchema.addField(DBHelper.COLUMN_NAME_OBJECT_ID, String.class);
            favouriteSchema.addField(DBHelper.COLUMN_NAME_PRONOUCE, String.class);
            favouriteSchema.addField(DBHelper.COLUMN_NAME_TYPE, String.class);
            favouriteSchema.addField(DBHelper.COLUMN_NAME_MEANING, String.class);
            oldVersion++;
        }
         if(oldVersion == 2){
                schema.create("History")
                .addField(DBHelper.COLUMN_NAME_ID, String.class)
                .addField(DBHelper.COLUMN_NAME_WORD, String.class)
                .addField(DBHelper.COLUMN_NAME_OBJECT_ID, String.class)
                .addField(DBHelper.COLUMN_NAME_PRONOUCE, String.class)
                .addField(DBHelper.COLUMN_NAME_TYPE, String.class)
                .addField(DBHelper.COLUMN_NAME_MEANING, String.class);
                oldVersion++;
            }
    }

But I am getting below error,

io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
    - Property 'History.id' has been changed from 'string' to 'int'.
    - Property 'History.word' has been made required.
    - Property 'History.objectId' has been made required.
    - Property 'History.pronunciation' has been made required.
    - Property 'History.type' has been made required.
    - Property 'History.meaning' has been made required.

I have not created any History table before, than why it is saying History.id has been changed from 'string' to 'int'?

How to add History table properly?


Solution

  •      if(oldVersion == 2){
                schema.create("History")
                .addField(DBHelper.COLUMN_NAME_ID, int.class)
                .addField(DBHelper.COLUMN_NAME_WORD, String.class, FieldAttribute.REQUIRED)
                .addField(DBHelper.COLUMN_NAME_OBJECT_ID, String.class, FieldAttribute.REQUIRED)
                .addField(DBHelper.COLUMN_NAME_PRONOUCE, String.class, FieldAttribute.REQUIRED)
                .addField(DBHelper.COLUMN_NAME_TYPE, String.class, FieldAttribute.REQUIRED)
                .addField(DBHelper.COLUMN_NAME_MEANING, String.class, FieldAttribute.REQUIRED);
                oldVersion++;
            }
    

    On your test phone, just uninstall/reinstall the app afterwards, or you'll need to bump the schema version and change the fields to the right type.