【发布时间】:2021-02-22 23:12:48
【问题描述】:
我有一个数据库表类,其中我确实更改了与索引相关的内容。以前的索引是这样的:
@Entity(indices = {@Index(value = {"jobNumber", "jobId"},
unique = true)})
但是我改成
@Entity(indices = {
@Index(value = "jobNumber", unique = true), @Index(value = "jobId", unique = true)
})
但是当我尝试迁移时,它给了我这样的问题:
caused by: java.lang.IllegalStateException: Migration didn't properly handle Job
我只需要使用第二种格式。我尝试添加迁移,但似乎不起作用。这是我的代码:
public static final Migration MIGRATION_4_5 = new Migration(4, 5) {
@Override
public void migrate(SupportSQLiteDatabase database) {
migrateJobTableForIndices(database);
}
};
private static void migrateJobTableForIndices(SupportSQLiteDatabase database) {
//create new table
database.execSQL(
"CREATE TABLE Job_new (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, jobId INTEGER NOT NULL, " +
"jobNumber TEXT)");
database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS index_Job_new_jobNumber_jobId ON Job_new (jobNumber, jobId)");
// Copy the data
database.execSQL(
"INSERT INTO Job_new (id, jobId, jobNumber) " +
"SELECT id, jobId, jobNumber " +
"FROM Job");
// Remove the old table
database.execSQL("DROP TABLE Job");
// Change the table name to the correct one
database.execSQL("ALTER TABLE Job_new RENAME TO Job");
}
有没有办法为更新的索引格式添加迁移。任何参考都非常感谢
【问题讨论】:
标签: android sqlite migration android-room