I am using Room on top of SQLite for Android, and I have the following two Pojo entities:
@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;
}
When I try to delete a record from the Event table, I get the following error:
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)
However, if I change the onDelete to CASCADE, no problem occurs!
Am I missing something?