0

I have a data class

@Entity(tableName = "type")
data class Type(
    @PrimaryKey(autoGenerate = true) var id: Int = 0,
    var type: Int = 0
)

When compiling project I receive message

Error:Room cannot pick a constructor since multiple constructors are suitable.

But if I change data class to

@Entity(tableName = "type")
data class Type(
    @PrimaryKey(autoGenerate = true) var id: Int = 0,
    var type: String = ""
)

or java class

@Entity(tableName = "type")
public class Type {
    @PrimaryKey(autoGenerate = true)
    private int id;
    private int type;
    // getters and setters
}

it works fine. Is it Kotlin bug or something else?

  • I cannot replicate the issue, whats your room version? – Tuby Jun 26 '17 at 08:55
  • 2
    This is probably because Kotlin is generating multiple Java constructors for your Type class when you have default arguments. This seems to be related to the issue described [here](https://stackoverflow.com/questions/44620835/room-cannot-pick-a-constructor-since-multiple-constructors-are-suitable-error) – Dr. Nitpick Jun 26 '17 at 13:23
  • @Tuby Room version is `1.0.0-alpha3` – Эльбек Джураев Jun 28 '17 at 09:43

1 Answers1

0

I do not know why this occurs, but if you use id? = 0 solves the problem, at least in the tests I did.

Android Studio Beta 7
ext.support_version = '26 .1.0 '
ext.kotlin_version = '1.1.51'
ext.anko_version = '0.10.1'
ext.archroom_version = '1.0.0-alpha9-1'

    @Entity(tableName = "type")
    data class Type(
            @PrimaryKey(autoGenerate = true) var id: Int? = 0,
            var type: Int = 0
    )
PCésar
  • 11
  • 2
  • 1
    This hapens because when you create the object it should have a null primary key, this only gets filled when you insert it into the database. I think you should default it to null, otherwise Room will probably update index 0 instead of inserting. – deive Jan 10 '18 at 14:17