onDelete = NO_ACTION causes error: "SQLiteConstraintException: FOREIGN KEY constraint failed" (code 787) onDelete = NO_ACTION causes error: "SQLiteConstraintException: FOREIGN KEY constraint failed" (code 787) sqlite sqlite

onDelete = NO_ACTION causes error: "SQLiteConstraintException: FOREIGN KEY constraint failed" (code 787)


If you will see here.

By default, NO_ACTION is used.

You don't need to provide it explicitly.

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

Also, You need to understand the Database is created once. You should reinstall the application and then check whether the delete works or Not.

Edited: As you have defined the ForeignKey but not onDelete CASCADE, You will have to remove all entries manually from Attendee which are pointing to the Event Table row- you are deleting.


Yep. @ForeignKey annotation helps to ensure that an entry can't get into an invalid state (for instance, foreign key can't point to an entry that does not exist).

Imagine you have Event(1, "some event") in your database as well as an attendee Attendee(1, 1). If the event is deleted, than the attendee entry will get into the invalid state, because it's foreign key eventId does not point to a valid entry. That's why you get an exception with description FOREIGN KEY constraint failed.

onDelete = CASCADE ensures that all the dependent entries will be erased with the referenced entry itself: in the described earlier case Attendee(1, 1) will just also get erased from the database when the referenced event entry (Event(1, "some event")) gets deleted.