gpt4 book ai didi

java - 安卓房间 : FOREIGN KEY constraint failed (Sqlite code 787)

转载 作者:行者123 更新时间:2023-11-29 02:24:24 27 4
gpt4 key购买 nike

我正在尝试创建两个表 dare_table 和 step_table,但在插入步骤时遇到了 FOREIGN KEY 约束失败错误 (787)。

我关注了这些帖子但没有成功:

我还是 Room 的新手,非常感谢任何帮助!

敢于上课:

@Entity(tableName = "dare_table",
indices = {@Index(value = {"stepList"}, unique = true),

public class Dare {

@PrimaryKey(autoGenerate = true)
public int id;

public Dare(String dareTitle, String stepList) {
this.dareTitle = dareTitle;
this.stepList = stepList;
}

public void setId(int id) {
this.id = id;
}

private String dareTitle;

@ColumnInfo(name = "stepList")
private String stepList;

public int getId() {
return id;
}

public String getDareTitle() {
return dareTitle;
}

public String getStepList() {
return stepList;
}
}

Step.class

@Entity(tableName = "step_table",
indices = {@Index(value = {"stepName"})},
foreignKeys = @ForeignKey(
entity = Dare.class,
parentColumns = "stepList",
childColumns = "stepName"
))

public class Step {

@PrimaryKey //Gets the same error with and without autoGenerate = true
public int id;

@ColumnInfo(name = "stepName")
private String stepName;

private boolean isCompleted;

public Step(String stepName, boolean isCompleted) {
this.stepName = stepName;
this.isCompleted = isCompleted;
}

public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}

public String getStepName() {
return stepName;
}

public boolean isCompleted() {
return isCompleted;
}
}

错误日志中引用的 doInBackground(在 Repository 类中)

    private static class InsertStepAsyncTask extends AsyncTask<Step, Void, Void> {

private StepDao stepDao;

private InsertStepAsyncTask(StepDao stepDao) {
this.stepDao = stepDao;
}

@Override
protected Void doInBackground(Step... steps) {
stepDao.insert(steps[0]);
return null;
}
}

错误日志:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: com.kyperstudios.daretowork, PID: 9845
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:365)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:784)
Caused by: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (Sqlite code 787), (OS error - 0:Success)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:818)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:803)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:51)
at androidx.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:64)
at com.kyperstudios.daretowork.Data.StepDao_Impl.insert(StepDao_Impl.java:89)
at com.kyperstudios.daretowork.Data.DareRepository$InsertStepAsyncTask.doInBackground(DareRepository.java:164)
at com.kyperstudios.daretowork.Data.DareRepository$InsertStepAsyncTask.doInBackground(DareRepository.java:154)
at android.os.AsyncTask$2.call(AsyncTask.java:345)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
at java.lang.Thread.run(Thread.java:784) 

最佳答案

您可以尝试插入一个名称为 Dare 类中不存在元素的外键的步骤。所以你必须先插入带有你想在步骤类中引用的 stepList 字段的 Dare 对象,然后你才能成功插入步骤

daredao().insertAll(new Dare("dare_title1", steps[0].getStepName()));
stepdao().insertAll(steps[0]);

关于java - 安卓房间 : FOREIGN KEY constraint failed (Sqlite code 787),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139256/

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