2

I am attempting my first Realm migration in one of my projects using Realm v 0.85.0. I am migrating from v 0.84.0 but the migration should also work for earlier versions. I have followed the example at https://github.com/realm/realm-java/tree/master/examples/migrationExample/src/main that was linked to in the documentation.

In this migration I am attempting to add two new tables. In order to add each new table my migration code looks like the following:

public class Migration implements RealmMigration {
     @Override
     public long execute(Realm realm, long version) {
        if (version == 0)
        {
           Table newTableOne = realm.getTable(NewTableOne.class);
           newTableOne.addColumn(ColumnType.STRING, "columnOne");
           // Add any other needed columns here and repeat process for NewTableTwo

           // Rest of migration logic goes here...

           version++;
        }

        return version;
      }
    }

According to Migration on Realm 0.81.1 the getTable() method will automatically create the table if it does not exist. I do not think this is the problem but I've included it just for completeness.

I am also attempting to add a couple of new columns to an existing table and set default values on these new columns. To do this I am using the following code:

Table existingTable = realm.getTable(ExistingTable.class);
existingTable.addColumn(ColumnType.BOOLEAN, "newColumnOne");
existingTable.addColumn(ColumnType.INTEGER, "newColumnTwo");
// Any other new columns needed here

long newColumnOneIndex = getIndexForProperty(existingTable, "newColumnOne");
long newColumnTwoIndex = getIndexForProperty(existingTable, "newColumnTwo");

for (int i = 0; i < existingTable.size(); i++)
{
    userTable.setBoolean(newColumnOneIndex, i, false);
    userTable.setLong(newColumnTwoIndex, i, 5);
}

The getIndexForProperty method is pulled directly from the example on Github and looks like:

private long getIndexForProperty(Table table, String name) {
        for (int i = 0; i < table.getColumnCount(); i++) {
            if (table.getColumnName(i).equals(name)) {
                return i;
            }
        }
        return -1;
    }

When this migration is run I am getting a RealmMigrationNeededException that states "Field count does not match - expected 22 but was 23". I have looked around StackOverflow and done some research via Google and on the Github wiki but have not been able to find any information relating to this exact "Field count does not match" message.

I have ensured that there is an addColumn line for each new field in each model class and there are not more fields in my model than I am adding columns and vice versa.

Any help that you may be able to provide would be greatly appreciated.

Community
  • 1
  • 1
Josh
  • 23
  • 5
  • Did you try to pull out the Realm file and open it in the Realm browser? – geisshirt Nov 23 '15 at 09:40
  • Btw, instead of calling `getIndexForProperty(table, name)`, you could use `table.getColumnIndex(name)`. – geisshirt Nov 23 '15 at 09:41
  • @geisshirt I have not tried to open it in the Realm browser. I am using a PC running Windows 7 and according to the docs there is not a version of the browser for Windows. – Josh Nov 23 '15 at 11:11
  • No, there is no Realm browser for Windows yet. You're welcome to send the Realm file to help@realm.io so I can take a look. – geisshirt Nov 23 '15 at 11:53
  • @geisshirt Come to think of it I might have an old copy of Snow Leopard that I had intended to install in VirtualBox to teach myself iOS / Obj-C. I'll check and see if I can get that running. If I can what would I be looking for in the Realm browser? If not I'll happily send the file over. Thanks for your prompt response! You've got a really great product here. – Josh Nov 23 '15 at 12:54
  • You should see your classes as tables and verify that you have the expected fields /columns. – geisshirt Nov 23 '15 at 13:16

0 Answers0