What does @ColumnInfo do

Alan Ramirez
1 min readOct 7, 2020

--

This annotation is used on values and variables inside your data classes, for example:

@Entity
data class Ingredient(
//Notice this var is not annotated
var name: String,
@ColumnInfo(name = "image")
val image: String?
) {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "ingredientID")
var ID: Long = 0
}

This Ingredient class is an Entity class, which makes it a table in your database, therefore every variable in this class by default will be a column in this table. By default the name of these columns will be the name of your variable. @ColumnInfo() allows you to mark these variables and pass in a name in the form of a string, this string will become the name of the column instead of the name of the variable.

It is good practice to have all of your variables in your data classes annotated with this even if the name you pass is the same as your variable. Reason being that if at any point you wish to change a variable’s name and it is not annotated by this tag you will have to increase the database version and that can lead to a lot of work if not errors.

--

--