I don't understand how use set_default
in a foreign key in onDelete.
Where do I set a default value for child row? Please tell me, what can I do?
I don't understand how use set_default
in a foreign key in onDelete.
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.
For example (in 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