I am creating an Entity (Room Persistence Library) class Food, where I want to make foodId
as autoincrement.
@Entity
class Food(var foodName: Str
Annotate your Entity class with the code below.
In Java:
@PrimaryKey(autoGenerate = true)
private int id;
In Kotlin:
@PrimaryKey(autoGenerate = true)
var id: Int
Room will then auto-generate and auto-increment the id field.
Its unbelievable after so many answers, but I did it little differently in the end. I don't like primary key to be nullable, I want to have it as first argument and also want to insert without defining it and also it should not be var.
@Entity(tableName = "employments")
data class Employment(
@PrimaryKey(autoGenerate = true) val id: Long,
@ColumnInfo(name = "code") val code: String,
@ColumnInfo(name = "title") val name: String
){
constructor(code: String, name: String) : this(0, code, name)
}