Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 841
Tuby Avatar asked Jun 07 '17 11:06

Tuby


People also ask

How do I allow null values in a room database?

In Kotlin, this means that title can never be null. To fix this, you just need to change the Entity class a bit, and allow the fields to be Nullable . val type: Int, val url: String?

What are the two annotations that every entity object class will have in room?

We'll use two annotations – @Embedded and @Relation. As I mentioned before – @Embedded allows nested fields to be referenced directly in the SQL queries. @Relation describes relations between two columns. We have to specify the parent column name, entity column name and entity class.

What is entity in Android room database?

When you use the Room persistence library to store your app's data, you define entities to represent the objects that you want to store. Each entity corresponds to a table in the associated Room database, and each instance of an entity represents a row of data in the corresponding table.


1 Answers

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

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

like image 179
Nokuap Avatar answered Sep 21 '22 18:09

Nokuap