gpt4 book ai didi

android - onDelete = NO_ACTION 导致错误 : "SQLiteConstraintException: FOREIGN KEY constraint failed" (code 787)

转载 作者:行者123 更新时间:2023-12-05 00:20:53 26 4
gpt4 key购买 nike

我在 SQLite for Android 上使用 Room,我有以下两个 Pojo 实体:

@Entity
public class Event {
@PrimaryKey
@NonNull
private String eventId;
private String eventName;
}
@Entity(primaryKeys = {"eventId","attendeeId"},
foreignKeys = {
@ForeignKey(entity = Event.class,
parentColumns = "eventId",
childColumns = "eventId" ,onDelete = NO_ACTION)
})
public class Attendee {
@NonNull
private String eventId;
@NonNull
private String attendeeId;
}

当我尝试从事件表中删除记录时,我收到以下错误:
Caused by: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787) at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:732)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeUpdateDelete(FrameworkSQLiteStatement.java:45)
at com.minapharm.hixspro.POJO.Room.Events.EventDao_Impl.deleteTable(EventDao_Impl.java:168)

但是,如果我将 onDelete 更改为 CASCADE,则不会出现问题!

我错过了什么吗?

最佳答案

如果你会看到 here .

By default, NO_ACTION is used.



您无需明确提供。
@Entity(primaryKeys = {"eventId","attendeeId"},
foreignKeys = {
@ForeignKey(entity = Event.class,
parentColumns = "eventId",
childColumns = "eventId")
})
public class Attendee {
@NonNull
private String eventId;
@NonNull
private String attendeeId;
}

此外,您需要了解数据库是创建一次的。您应该重新安装应用程序,然后检查删除是否有效。

已编辑:由于您已定义 ForeignKey 但未定义 onDelete CASCADE,因此您必须手动从 Attendee 中删除所有指向 Event Table 行的条目 - 您正在删除。

关于android - onDelete = NO_ACTION 导致错误 : "SQLiteConstraintException: FOREIGN KEY constraint failed" (code 787),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56787511/

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