Room cannot pick a constructor since multiple constructors are suitable error
This worked for me:
@Entity
data class Site(
@PrimaryKey(autoGenerate = true) var id: Long = 0),
var name: String = "",
var url: String = "",
@Ignore var ignored: String? = null
)
I had this error because Kotlin apparently generates multiple Java constructors for a single Kotlin constructor with default argument values. Working code see next:
@Entity
data class Site(
var name: String,
var url: String,
@PrimaryKey(autoGenerate = true) var id: Long)
None of the above solutions are good, since they work but may cause errors.
Kotlin's Data Class generates several Methods using the default constructor. That means that equals(), hashCode(), toString(), componentN() functions and copy() is generated using the attributes you assign to your constructor.
Using the above solutions like
@Entity data class Site(@PrimaryKey(autoGenerate = true) var id: Long) {
@Ignore constructor() : this(0)
var name: String = ""
var url: String = ""
}
generates all the above listed methods only for id. Using equals leads to unwanted quality, same as toString(). Solving this requires you to have all attributes you want to process inside the constructor and add a second constructor using ignore like
@Entity data class Site(
@NonNull @PrimaryKey(autoGenerate = true) var id: Long,
var name: String = "",
var url: String = "") {
@Ignore constructor(id = 0, name = ", url = "") : this()
}
You should really keep in mind, that you usually use data classes to have methods like toString and copy. Only this solution is working to avoid unwanted bugs during runtime.