如何在Room Android中使用set_default设置外键?

3

我不理解如何在onDelete中使用set_default来设置外键的默认值。

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

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 问题及其已接受的答案:Room 数据库:删除时 NOT NULL 约束失败


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接