【发布时间】:2020-05-25 16:49:50
【问题描述】:
我收到一个错误
error: BookGroupEntry has a foreign key (bookGroupParameterId) that references BookGroupParameter (id) but BookGroupParameter does not have a unique index on those columns nor the columns are its primary key. SQLite requires having a unique constraint on referenced parent columns so you must add a unique index to BookGroupParameter that has (id) column(s).
这完全让我难以置信,因为我没有错误报告的情况
我的BookGroupParameter表如下
@Entity(
tableName = "BookGroupParameters",
primaryKeys = ["id", "parameterId", "bookGroupId"],
indices = [Index("id")],
foreignKeys = [
ForeignKey(
entity = Parameter::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("parameterId"),
onDelete = ForeignKey.CASCADE
),
ForeignKey(
entity = BookGroup::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("bookGroupId"),
onDelete = ForeignKey.CASCADE
)]
)
data class BookGroupParameter(
@NonNull
var id: Int,
val bookGroupId: Int,
val parameterId: Int
) {
}
这是我的 BookGroupEntry 表
@Entity(
tableName = "BookGroupEntries",foreignKeys = [
ForeignKey(
entity = BookGroupParameter::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("bookGroupParameterId"),
onDelete = ForeignKey.CASCADE
)]
)
data class BookGroupEntry(
@PrimaryKey
var id: Int,
var value: String,
val bookGroupParameterId:Int
)
为什么它告诉我 BookGroupEntry 中的 FK 未定义为 BookGroupParameters 表中的 PK?
【问题讨论】:
标签: android kotlin android-room android-jetpack