No "NOT NULL" and "UNIQUE" constraint on Room Persistence Library

raggedycoder picture raggedycoder · Sep 4, 2017 · Viewed 20.4k times · Source

While playing with the Room Persistence Library I came to know that there is no methodology to set a data class field with NOT NULL and also UNIQUE constraints. whether SQLite supports those constraints. Isn't it a problem to migrate old database where those constraints are used? Can anyone give a suggestion for this issue?

Answer

CommonsWare picture CommonsWare · Sep 4, 2017

I came to know that there is no methodology to set a data class field with NOT NULL and also UNIQUE constraints

A @NonNull annotation on an @Entity field will cause that field's column to have NOT NULL applied to it.

unique=true on an @Index will enforce a uniqueness constraint (e.g., @Entity(indices={@Index(value="something", unique=true)}). However, you are correct that a plain UNIQUE constraint on a column, other than via an index, is not supported.

Isn't it a problem to migrate old database where those constraints are used?

Room is not designed to support existing database structures, particularly in the now-current alpha state. Over time, I would expect Room to support a higher percentage of SQLite features, though I will be stunned if it ever reaches 100%.