realm-java: RealmMigrationNeededException: Field count is more than expected - expected 10 but was 11

Hi, I have problem with migration in my application. All time logcat gives me “io.realm.exceptions.RealmMigrationNeededException: Field count is more than expected - expected 10 but was 11 after migration”.

My old model:

    @PrimaryKey
    private int sid;
    private String title;
    private String noSpecialCharsTitle;
    private String lang;
    private String img;
    private String icon;
    private boolean owned;
    private boolean favorite;
    private int favouritePosition = Integer.MAX_VALUE;

My new model:

    @PrimaryKey
    private int sid;
    private String title;
    private String noSpecialCharsTitle;
    private String lang;
    private String img;
    private String icon;
    private boolean owned;
    private boolean favorite;
    private int favouritePosition = Integer.MAX_VALUE;
    private int stationPosition = Integer.MAX_VALUE;

Migration:

        RealmConfiguration realmConfig =  new RealmConfiguration.Builder()
                .schemaVersion(SCHEMA_VERSION)
                .migration((realm, oldVersion, newVersion) -> {
                    if(oldVersion == 0) {
                        final RealmObjectSchema station = realm.getSchema().get("Station");
                        station.addField("stationPosition", int.class);
                        oldVersion++;
                    }
                })
                .build();
        Realm.setDefaultConfiguration(realmConfig);

What’s wrong with this migration? Why app always throws RealmMigrationNeededException?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 27 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Realm.init(context);
Realm.setDefaultConfiguration(new RealmConfiguration.Builder()
                                                       .deleteIfMigrationNeeded()
                                                       .build());
Realm realm = Realm.getDefaultInstance();`

You may as well just set deleteIfMigrationNeeded() on the RealmConfiguration then

If you have a Realm file, model classes and migration steps which can reproduce the error, we will appreciate if you can share it all.

@kneth Fortunately i’m MacOS user. I opened database in Realm Browser. I found 11th field - ‘position’. In migration i don’t have this field. I changed stationPosition to position in model and now everything works fine, but it’s a little odd. image