领域:未为字段定义索引

时间:2015-09-22 15:13:14

标签: android realm

我正在编写一个RealmMigration,并且经过几次不同的错误之后,我想我终于得到了它,但现在我得到了io.realm.exceptions.RealmMigrationNeededException:索引没有定义为字段&#39 ;的PrimaryKey'

我看到了一些需要使用table.addSearchIndex()的内容,但即使在为我的每个表添加后,我仍然会得到异常。

这是我的移民课程。

Migration.java

public class Migration implements RealmMigration {

@Override
public long execute(Realm realm, long version) {
    Timber.i("Current database version: " + version);

    /**
     * Version 1:
     *      Task:
     *          Remove boolean field completed
     *          Remove Date field completedDate
     *      Transaction:
     *          Add int field type
     */
    if (version == 0) {
        // Transaction
        Table transactionTable = realm.getTable(Transaction.class);
        transactionTable.addColumn(ColumnType.INTEGER, "type");
        long transactionPrimaryKeyIndex = getIndexForProperty(transactionTable, "primaryKey");
        long transactionTitleIndex = getIndexForProperty(transactionTable, "title");
        long transactionPointsIndex = getIndexForProperty(transactionTable, "points");
        long transactionDateIndex = getIndexForProperty(transactionTable, "date");
        long transactionTypeIndex = getIndexForProperty(transactionTable, "type");

        for (int i = 0; i < transactionTable.size(); i++) {
            // Until now the only possible transaction was reward
            transactionTable.setLong(transactionTypeIndex, i, Transaction.TYPE_REWARD);
        }

        // Task
        Table taskTable = realm.getTable(Task.class);

        // Go through and create Transactions for each completed Task
        long taskPrimaryKeyIndex = getIndexForProperty(taskTable, "primaryKey");
        long taskCompletedIndex = getIndexForProperty(taskTable, "completed");
        long taskCompletedDateIndex = getIndexForProperty(taskTable, "completedDate");
        long taskTitleIndex = getIndexForProperty(taskTable, "title");
        long taskPointsIndex = getIndexForProperty(taskTable, "points");


        for (int i = 0; i < taskTable.size(); i++) {
            if (taskTable.getBoolean(taskCompletedIndex, i)) {
                transactionTable.addEmptyRowWithPrimaryKey(transactionTable.getLong(transactionPrimaryKeyIndex, transactionTable.size() - 1) + 1);
                long j = transactionTable.size() - 1; // The new row

                transactionTable.setString(transactionTitleIndex, j, taskTable.getString(taskTitleIndex, i));
                transactionTable.setLong(transactionPointsIndex, j, taskTable.getLong(taskPointsIndex, i));
                transactionTable.setDate(transactionDateIndex, j, taskTable.getDate(taskCompletedDateIndex, i));
                transactionTable.setLong(transactionTypeIndex, j, Transaction.TYPE_TASK);
            }
        }

        // Finally, remove the columns we don't need any more
        taskTable.removeColumn(getIndexForProperty(taskTable, "completed"));
        taskTable.removeColumn(getIndexForProperty(taskTable, "completedDate"));

        // https://realm.io/news/realm-java-0.82.0/
        taskTable.addSearchIndex(taskPrimaryKeyIndex);
        transactionTable.addSearchIndex(transactionPrimaryKeyIndex);

        Table reminderTable = realm.getTable(Reminder.class);
        Table rewardTable = realm.getTable(Reward.class);
        reminderTable.addSearchIndex(getIndexForProperty(reminderTable, "primaryKey"));
        rewardTable.add(getIndexForProperty(rewardTable, "primaryKey"));

        version++;
    }

    return version;
}

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;
}

}

Task.java

public class Task extends RealmObject {
    @PrimaryKey
    private long primaryKey;
    private String title;
    private String description;
    private int points;
    private boolean hasReminders;
    private RealmList<Reminder> reminders;

    public static long getNextPrimaryKey(Realm realm) {
        RealmResults<Task> tasks = realm.where(Task.class).findAllSorted("primaryKey");
        if (tasks.size() == 0) return 0;
        return tasks.last().getPrimaryKey() + 1;
    }

    // Getters and setters
}

Transaction.java

public class Transaction extends RealmObject {
    public static final int TYPE_TASK = 0;
    public static final int TYPE_REWARD = 1;

    @PrimaryKey
    private long primaryKey;
    private String title;
    private int points;
    private Date date;
    private int type;

    public static long getNextPrimaryKey(Realm realm) {
        if (realm != null) {
            RealmResults<Transaction> transactions = realm.where(Transaction.class).findAllSorted("primaryKey");
            if (transactions.size() == 0) return 0;
                return transactions.last().getPrimaryKey() + 1;
            } else {
                return 0;
            }
        }
    }

    // Getters and setters
}

1 个答案:

答案 0 :(得分:1)

这是因为您在添加索引之前删除了任务表中的两列。这意味着您在开头计算的索引不再有效。