[英]Database Migration Failed due to unique constraint in room
I am having unique constraint in one of my table in my old database. 我在旧数据库中的一个表中有唯一约束。 While migrating to room, I have created new table as per instruction given on [link][1] and applied unique constraint using "indices" keyword in "TaskDetail" entity class.
在迁移到房间时,我根据[link] [1]上给出的指令创建了新表,并在“TaskDetail”实体类中使用“indices”关键字应用了唯一约束。 And provided empty migration.
并提供空迁移。 while running migration test i get error related to unique constraint as mentioned below.
在运行迁移测试时,我得到与唯一约束相关的错误,如下所述。 Am I doing any thing wrong??
我做错了什么?
Database Schema
数据库架构
String CREATE_TABLE_TASK = "CREATE TABLE IF NOT EXISTS "+TASK+
" (`task_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
"`task_note` TEXT," +
"`status` INTEGER NOT NULL)";
String CREATE_TABLE_TASK_DETAIL = "CREATE TABLE IF NOT EXISTS "+TASK_DETAIL+
" (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
"`detail` TEXT NOT NULL UNIQUE)";
Room Entity Tables
房间实体表
@Entity(tableName = "task_master")
public class Task {
@ColumnInfo(name = "task_id")
@PrimaryKey(autoGenerate = true)
@NonNull
private int taskId;
@ColumnInfo(name = "task_note")
private String task;
private @TaskStatus
int status;
}
//TOKEN DETAIL TABLE
@Entity(tableName = "task_detail",indices ={@Index(name = "detail",value = "detail",unique = true)})
public class TaskDetail {
@PrimaryKey(autoGenerate = true)
private int id;
@NonNull
private String detail;
}
RoomDatabase class
RoomDatabase类
@Database(entities = {Task.class, TaskDetail.class},version = 2,exportSchema = true)
public abstract class AppDatabase extends RoomDatabase{
private static final Object sObject = new Object();
private static AppDatabase sInstance;
public abstract TaskDao getTaskDao();
public static AppDatabase getInstance(Context context){
if(sInstance == null){
synchronized (sObject){
if(sInstance == null){
sInstance = Room.databaseBuilder(context,AppDatabase.class,"task.db")
.allowMainThreadQueries()
.build();
}
}
}
return sInstance;
}
}
Error
错误
expacted:TableInfo{name='task_detail', columns={id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, detail=Column{name='detail', type='TEXT', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[Index{name='detail', unique=true, columns=[detail]}]}
found:TableInfo{name='task_detail', columns={id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, detail=Column{name='detail', type='TEXT', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
You have to define unix index for your tables. 您必须为表定义unix索引。 For
task_master
if your unique key is task_id
add something like: 对于
task_master
如果您的唯一键是task_id
添加以下内容:
database.execSQL("CREATE UNIQUE INDEX `index_task_master_task_id` ON `task_master` (`task_id`)");
Same as for the task_detail
table 与
task_detail
表相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.