简体   繁体   English

如何在 Room Android 的外键中使用 set_default

[英]How use set_default in foreign key in Room Android

I don't understand how use set_default in a foreign key in onDelete.我不明白如何在 onDelete 的外键中使用set_default

Where do I set a default value for child row?我在哪里设置子行的默认值? Please tell me, what can I do?请告诉我,我能做什么?

They are added at the variable declaration in the Entity annotated class.它们被添加到 Entity 注释类中的变量声明中。

For example (in Kotlin):例如(在 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"

) { /*...*/ }

The value set for the variables will act as default values.为变量设置的值将作为默认值。

For extra reference on SET_DEFAULT see this other SO Question with it's accepted answer: Room database: NOT NULL constraint failed at deletion有关 SET_DEFAULT 的额外参考,请参阅此其他 SO 问题及其已接受的答案: 房间数据库:删除时非空约束失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM