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.