【问题标题】:Unable to add indices while migrating room database迁移房间数据库时无法添加索引
【发布时间】:2019-07-28 13:31:54
【问题描述】:

我正在将房间数据库版本从 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

【问题讨论】:

  • 您找到解决方案了吗?我也面临同样的问题。

标签: java sqlite android-sqlite android-room


【解决方案1】:

为 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 {
    .......
}

【讨论】:

  • 以同样的方式做,但它仍然给我迁移数据库的错误
猜你喜欢
  • 2018-07-02
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
  • 2021-08-13
  • 2019-07-23
  • 2018-12-01
  • 2021-09-02
  • 1970-01-01
相关资源
最近更新 更多