3

我不明白如何set_default在 onDelete 中使用外键。

在哪里为子行设置默认值?请告诉我,我能做什么?

4

1 回答 1

0

它们被添加到实体注释类的变量声明中。

例如(在 Kotlin 中):

@Entity
data class MyClass (

    var id: Long = 0L

    // index is recommended for foreign keys to avoid full table scans on 'child column's table')
    @ColumnInfo (name = "name", index = true)
    var name_ChildRow: String = "default-name"

    // Explicit defaultValue in column info, excluding this could give Not Null constraint fail exceptions
    // in onDeletes and onUpdates set_default but shouldn't actually be needed.
    // (In case room generates a query which states something other; this default value
    // in columnInfo annotation should enforce the default value).
    @ColumnInfo (name = "parent_id", defaultValue = "1", index = true)
    var yetAnotherTableParentId: Long = "1L"

    @ColumnInfo (name = "string_null")
    var stringNull: String = "Null"

) { /*...*/ }

为变量设置的值将作为默认值。

有关 SET_DEFAULT 的额外参考,请参阅其他 SO 问题及其已接受的答案:房间数据库:NOT NULL 约束在删除时失败

于 2019-11-05T17:07:12.590 回答