如何在Room Android

时间:2018-04-18 23:22:34

标签: android foreign-keys android-room setdefault

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

我在哪里设置子行的默认值?请告诉我,我该怎么办?

1 个答案:

答案 0 :(得分:0)

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

例如(在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问题及其接受的答案:Room database: NOT NULL constraint failed at deletion