I have 3 entities - 1 child and 2 parent. 2 parent entity may have many child entities, everyone has their own.
This is child:
@Entity(
tableName = "share",
foreignKeys = [
ForeignKey(
entity = Pool::class,
childColumns = ["entity_id"],
parentColumns = ["id"],
onDelete = CASCADE
),
ForeignKey(
entity = Note::class,
childColumns = ["entity_id"],
parentColumns = ["id"],
onDelete = CASCADE
)
]
)
data class Share(
@ColumnInfo(name = "share_id")
@PrimaryKey(autoGenerate = false)
val shareId: String,
@ColumnInfo(name = "entity_id")
val entityId: String,
@ColumnInfo(name = "entityType")
val entityType: Int
)
And this is parents:
@Entity(tableName = "pool")
data class Pool(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "id")
val poolId: String,
@ColumnInfo(name = "category")
val type: Int
)
@Entity(tableName = "note")
data class Note(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "id")
val noteId: String
)
Pool and Note can have several Share, which do not intersect, each of them has its own and unique.
But when i try to save Share i have next error:
W/System.err: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)
W/System.err: at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
W/System.err: at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:783)
W/System.err: at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
W/System.err: at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
W/System.err: at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:50)
W/System.err: at android.arch.persistence.room.EntityInsertionAdapter.insertAndReturnIdsList(EntityInsertionAdapter.java:243)
W/System.err: at com.my_app.data.db.dao.share.ShareDao_Impl.insertShare(ShareDao_Impl.java:114)
How to avoid this error?