column references a foreign key but it is not part of an index
我们在run app的过程中,系统提示如下问题:
Warning:[...] column references a foreign key but it is not part of an index. This may trigger full table scans whenever parent table is modified so you are highly advised to create an index that covers this column.
比如如下代码:
@Entity(
tableName = "filtered_text_model",
foreignKeys = [
ForeignKey(
entity = Scan::class,
parentColumns = ["scan_id"],
childColumns = ["scan_id"],
onDelete = ForeignKey.CASCADE
)
]
)
data class FilteredTextModel(
@ColumnInfo(name = "filtered_text_model_id") @PrimaryKey(autoGenerate = true) val filteredTextModelId: Int = 0,
@ColumnInfo(name = "scan_id") val scanId: Int,
@ColumnInfo(name = "type") val type: String,
@ColumnInfo(name = "content") val content: String
) {
}
上面报错中:[...]
这里指的是scan_id
解决方案如下:
将
@ColumnInfo(name = "scan_id") val scanId: Int,
修改为
@ColumnInfo(name = "scan_id", index = true) val scanId: Int,
。
在stackoverflow
中有一个类似的问题:链接。