Android 房间中的复合键关系

Android 房间中的复合键关系,android,kotlin,android-room,android-jetpack,Android,Kotlin,Android Room,Android Jetpack,我犯了一个错误 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 c

我犯了一个错误

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?

至于我,错误消息很有意义:

  • 索引(“id”)实际上不是唯一的索引
  • “id”列不是BookGroupParameter表中的主键(因为它的主键是您编写的复合键)
要消除错误,您可以:

  • 要使表中的索引BookGroupParameter唯一,请执行以下操作:
  • 要将BookGroupParameter的复合主键放入BookGroupEntry

  • 对于我来说,错误消息是有意义的:

    • 索引(“id”)实际上不是唯一的索引
    • “id”列不是BookGroupParameter表中的主键(因为它的主键是您编写的复合键)
    要消除错误,您可以:

  • 要使表中的索引BookGroupParameter唯一,请执行以下操作:
  • 要将BookGroupParameter的复合主键放入BookGroupEntry

  • 绝对正确。然而,错误不应该说“id”不是主键。它可以使用其他一些词语,比如您需要指定组合中的哪个PK是唯一的。谢谢,完全正确。然而,错误不应该说“id”不是主键。它可以使用其他一些词语,比如您需要指定组合中的哪个PK是唯一的。谢谢兄弟
        indices = [Index("id", unique = true)]
    
    @Entity(
        tableName = "BookGroupEntries",foreignKeys = [
            ForeignKey(
                entity = BookGroupParameter::class,
                parentColumns = arrayOf("id","bookGroupId","parameterId"),
                childColumns = arrayOf("bookGroupParameterId","bookGroupParameterBookGroupId","bookGroupParameterParameterId"), // changed
                onDelete = ForeignKey.CASCADE
            )]
    )
    data class BookGroupEntry(
        @PrimaryKey
        var id: Int,
        var value: String,
        val bookGroupParameterId:Int,
        val bookGroupParameterBookGroupId:Int, // added
        val bookGroupParameterParameterId:Int // added
    )