gpt4 book ai didi

android - 由于空间中的唯一约束,数据库迁移失败

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:34 25 4
gpt4 key购买 nike

我在旧数据库中的一个表中有唯一约束。在迁移到房间时,我按照 [link][1] 上给出的说明创建了新表,并在“TaskDetail”实体类中使用“indices”关键字应用了唯一约束。并提供空迁移。在运行迁移测试时,我收到与唯一约束相关的错误,如下所述。我做错了什么吗??

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

@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=[]}

最佳答案

你必须为你的表定义 unix 索引。对于 task_master 如果您的唯一键是 task_id 添加如下内容:

database.execSQL("CREATE UNIQUE INDEX `index_task_master_task_id` ON `task_master` (`task_id`)");

task_detail 表相同

关于android - 由于空间中的唯一约束,数据库迁移失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47688914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com