【发布时间】:2018-04-18 23:22:34
【问题描述】:
我不明白如何在 onDelete 的外键中使用 set_default。
在哪里为子行设置默认值?请告诉我,我该怎么办?
【问题讨论】:
标签: android foreign-keys android-room setdefault
我不明白如何在 onDelete 的外键中使用 set_default。
在哪里为子行设置默认值?请告诉我,我该怎么办?
【问题讨论】:
标签: android foreign-keys android-room setdefault
它们被添加到实体注释类中的变量声明处。
例如(在 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
【讨论】: