How to annotate Column as NOT NULL using Android Room Persistence Library

Tuby picture Tuby · Jun 7, 2017 · Viewed 28.2k times · Source

My data class looks like this

@Entity(tableName = "items")
data class Item(
        @ColumnInfo(name = "name") var name: String = "",
        @ColumnInfo(name = "room") var room: String = "",
        @ColumnInfo(name = "quantity") var quantity: String = "",
        @ColumnInfo(name = "description") var description: String = "",
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "id") var id: Long = 0
)

Room uses SQLite and SQLite supports NOT NULL columns in its database. I tried annotating columns with @NonNull but it has no effect.

Is there a way to make columns in Room Database not nullable?

Answer

Nokuap picture Nokuap · Nov 4, 2017

For Java you should annotate with @android.support.annotation.NonNull

DO NOT CONFUSE WITH @io.reactivex.annotations.NonNull