How to annotate a default value inside a android room entity?
With the release of room persistence 2.2.0, there's a new property added to @ColumnInfo annotation which can be used to specify the default value of a column. See documentation.
@Entity(tableName = "users")
data class User(
@PrimaryKey val id: Long,
@ColumnInfo(name = "user_name", defaultValue = "temp") val name: String
@ColumnInfo(name = "last_modified", defaultValue = "CURRENT_TIMESTAMP" ) val lastModified: String
)
You can set the default value using @ColumnInfo
annotation-
@ColumnInfo(defaultValue = "No name")
public String name;
and
@ColumnInfo(defaultValue = "0")
public int flag;
or for any kind of data types check the reference from here Google developer doc
Room hasn't any annotation for default value, but you can set default value in your entity like this:
@Entity(tableName = "MyTable")
class MyClass {
...
public String MyDefaultValuedCol = "defaultString";
public boolean MyDefaultFlagCol = true;
}