Android Room Add Unique Contraint to Table (Kotlin)

October 21, 2018

Single column unique (e.g. key only).

@Entity(tableName = "item", indices = [Index(value = "id_key", unique = true)])
data class Item (

}

Combine two columns to be unique (e.g. type and key).

@Entity(tableName = "item", indices = [Index(value = ["item_type", "id_key"], unique = true)])
data class Item (

}

Multiple unique constaints.

@Entity(tableName = "item", indices = [Index(value = "id_key", unique = true), Index(value = ["item_type", "another_key"], unique = true)])
data class Item (

}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.