迁移会议室数据库时无法添加索引

时间:2019-07-28 13:31:54

标签: java android database sqlite

我正在将会议室数据库的版本从1迁移到版本2。在迁移时,我已在迁移代码中添加了索引,但无法正常工作。

下面是代码:

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {

             database.execSQL("CREATE TABLE student (" +
                    "student_id INTEGER PRIMARY KEY NOT NULL ," +
                    "student_name TEXT," +
                    "student_roll_no TEXT," +
                    "department_id INTEGER, FOREIGN KEY (department_id) REFERENCES department(dept_id)" +
                    ")");
            database.execSQL("CREATE INDEX `index_student_department_id` ON `student` (`department_id`)");


        }
    };

结果为:-

Expected indices=[Index{name='index_student_department_id', unique=false, columns=[department_id]}]
Result Found:indices=null

1 个答案:

答案 0 :(得分:0)

使Student表的实体具有创建索引的索引。消息说应该是,但 indices ,但什么也没找到。

@Entity(indices = {@Index(name="`index_student_department_id`", unique = false, value =  {"`department_id`"})})
public class Student {
    .......
}

或(默认为unique = false

@Entity(indices = {@Index(name="`index_student_department_id`", value =  {"`department_id`"})})
public class Student {
    .......
}