具有一对一关系外键约束的会议室数据库失败错误

时间:2019-01-21 18:29:40

标签: java android foreign-keys android-room one-to-one

我有一个包含两个表Student和Course的数据库。我可以在学生表中插入数据,但是当我尝试将数据插入到“课程表”应用程序崩溃时。这是logcat:

2019-01-21 19:56:22.302 28939-29121/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.rodroiddev.catalogulinstructoruluiauto, PID: 28939
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:353)
    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:245)
    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:764)
 Caused by: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)
    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
    at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:50)
    at android.arch.persistence.room.EntityInsertionAdapter.insertAndReturnId(EntityInsertionAdapter.java:114)
    at com.rodroiddev.catalogulinstructoruluiauto.db.CourseDao_Impl.insert(CourseDao_Impl.java:104)
    at com.rodroiddev.catalogulinstructoruluiauto.course.CourseRepository$InsertCourseAsyncTask.doInBackground(CourseRepository.java:53)
    at com.rodroiddev.catalogulinstructoruluiauto.course.CourseRepository$InsertCourseAsyncTask.doInBackground(CourseRepository.java:44)
    at android.os.AsyncTask$2.call(AsyncTask.java:333)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
    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:764) 

这是插入代码:

private static class InsertCourseAsyncTask extends AsyncTask<Course, Void, Void> {
    private CourseDao courseDao;

    public InsertCourseAsyncTask(CourseDao courseDao) {
        this.courseDao = courseDao;
    }

    @Override
    protected Void doInBackground(Course... courses) {
        courseDao.insert(courses[0]);
        return null;
    }
}

Course.java

@Entity(tableName = "course_table",
    foreignKeys = @ForeignKey(entity = Student.class,
            parentColumns = "sId",
            childColumns = "studentId",
            onDelete = CASCADE),
            indices = {@Index("studentId")})

公共课课程{

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "cId")
public int id;

public String kmStart;

public String  kmStop;

@ColumnInfo(name = "studentId")
public int studentId;

public Course(String kmStart, String kmStop) {

    this.kmStart = kmStart;
    this.kmStop = kmStop;

}

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

public int getId() {
    return id;
}

public String  getKmStart() {
    return kmStart;
}

public String getKmStop() {
    return kmStop;
}

public void setStudentId(int studentId) {
    this.studentId = studentId;
}

public int getStudentId() {
    return studentId;
}

}

CourseDao

@Dao

公共界面CourseDao {

@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(Course... courses);

@Update(onConflict = OnConflictStrategy.IGNORE)
void update(Course course);

@Delete
void delete(Course course);

@Query("DELETE FROM course_table")
void deleteAllCourses();

@Query("SELECT * FROM course_table ORDER BY kmStart ASC")
LiveData<List<Course>> getAllCourses();

}

我希望您可以根据我在此处发布的信息为我提供帮助。 谢谢!

1 个答案:

答案 0 :(得分:1)

是否在保存之前将studentId设置为课程对象?如果您不这样做,它将崩溃,因为它将尝试为StudentId添加0作为不存在的外键值。

将“课程”对象的“ studentId”设置为“学生”对象的id。并先保存学生,以便获得ID