1

per Annotate indices and uniqueness, seems it could add constrain unique like below,

but got error in AndroidStudio 3 (the editor has red underline saying "unresolved the reference:indices"), :

/Users/application/database/DbAuthor.kt
Error:(14, 39) Expecting an element

@Entity(tableName = "author" indices = arrayOf(Index(value = "authorid", unique = true)))
class DbAuthor(

    @ColumnInfo(name = "authorid")
    var authorid: Int? = null,

how to add unique constrain on a field when using kotlin?

gradle is like:

buildscript {
ext {
    verAndroidSupport = '27.0.0'
    arch = "1.0.0"
    archRoomVersion = "1.0.0"

}
ext.kotlin_version = '1.2.20'

and

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'


//arch
implementation "android.arch.lifecycle:runtime:${arch}"
implementation "android.arch.lifecycle:extensions:${arch}"
annotationProcessor "android.arch.lifecycle:compiler:${arch}"
//room
compile "android.arch.persistence.room:runtime:${archRoomVersion}"
annotationProcessor "android.arch.lifecycle:compiler:${arch}"

annotationProcessor "android.arch.persistence.room:compiler:${archRoomVersion}"
kapt "android.arch.persistence.room:compiler:1.0.0-alpha1"

1 Answer 1

9

You missed , after "author"
@Entity(tableName = "author", indices = arrayOf(Index(value = "authorid", unique = true)))

2
  • 3
    getting error : Assigning single elements to varargs in named form is forbidden Jun 26, 2019 at 6:50
  • @Entity(indices = arrayOf(Index(value = arrayOf("authorid"), unique = true))) Feb 18, 2020 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.