Android Room Add Unique Contraint to Table (Kotlin)

Oct 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 (}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.