The room cannot select a constructor, since suitable errors are multiple constructors

I am trying to implement a persistent library in my android kotlin project, but catch this error at compile time:

error: the number cannot be chosen by the constructor, since several constructors are suitable. Try annotating unwanted constructors with @Ignore.

Error code:

@Entity data class Site( var name: String = "", var url: String = "", @PrimaryKey(autoGenerate = true) var id: Long = 0) 
+20
android constructor kotlin android-room android-components
source share
9 answers

I had this error because Kotlin apparently generates several Java constructors for one Kotlin constructor with default argument values. Working code see below:

 @Entity data class Site( var name: String, var url: String, @PrimaryKey(autoGenerate = true) var id: Long) 
+13
source share

None of the above solutions are suitable, as they work, but can lead to errors.

The Kotlin data class generates several methods using the default constructor. This means that the functions equals () , hashCode () , toString () , componentN (), and copy () are generated using the attributes that you assign to your constructor.

Using the above solutions, such as

 @Entity data class Site(@PrimaryKey(autoGenerate = true) var id: Long) { @Ignore constructor() : this(0) var name: String = "" var url: String = "" } 

generates all of the above methods only for id. Using equals results in undesirable quality, as does toString (). To solve this problem, you need to have all the attributes that you want to handle 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 keep in mind that you usually use data classes to have methods like toString and copy. Only this solution works to avoid unwanted runtime errors.

+6
source share

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 ) 
+2
source share

it works for me

 @Entity data class TaskDetail @Ignore constructor( @PrimaryKey(autoGenerate = true) var id:Long = 0, var taskId:Long = 0, var content:String = "") { constructor():this(id = 0) } 

I use @Ignore to disable ROOM warning

There are some good constructors, and Room will choose the constructor with no arguments. You can use the @Ignore annotation to remove unnecessary constructors.

And add a default constructor for ROOM.

+1
source share

Try changing the data type of the variable from val to var:

BEFORE :

  @Entity data class Product( @PrimaryKey val id: String = "", val name: String = "" ) 

AFTER:

 @Entity data class Product( @PrimaryKey var id: String = "", var name: String = "" ) 
+1
source share

Here you change your version application database and restart the agian program, it will work:

 @Database(entities = arrayOf(Site::class), version = 123) abstract class YourAppDatabase : RoomDatabase() { abstract fun yourDao(): YourDao } 

and you can also try this data class :

  @Entity data class Site(@PrimaryKey(autoGenerate = true) var id: Long) { @Ignore constructor() : this(0) var name: String = "", var url: String = "", } 

and the last instruction: your id primary key must be manually incremented .

Hope this works for you. :)

A test to show that the above answers are not valid.

 data class TestModel(var id: Int = 0) { constructor() : this(0) var name: String = "defaultname" var testData: String = "defaulttestData" } val testModel = TestModel(5) testModel.name = "test" val testModel2 = TestModel(5) testModel2.testData = "testdata" testModel2.name = "test" info { "Test with name set: $testModel" } info { "Testdata equals Testdata2 ${testModel.equals(testModel2)}" } 

returns Test with a set of names: TestModel (id = 5) and Testdata is Testdata2 true

0
source share

Just leave my answer in case this helps someone. I ran into the same problem, none of the answers above worked. The only thing that worked was the transition from data class to class . I invite anyone to try the same code and explain why it succeeded:

Before

 @Entity data class ImgurGalleryPost ( @NotNull @PrimaryKey var id: String, var title: String?, var description: String?, var datetime: Int?, var cover: String?, var coverWidth: Int?, var coverHeight: Int?, var accountUrl: String?, var accountId: Int?, var privacy: String?, var layout: String?, var views: Int?, var link: String?, var ups: Int?, var downs: Int?, var points: Int?, var score: Int?, var isAlbum: Boolean?, var vote: Boolean?, var favorite: Boolean?, var nsfw: Boolean?, var section: String?, var commentCount: Int?, var favoriteCount: Int?, var topic: String?, var topicId: Int?, var imagesCount: Int?, var inGallery: Boolean?, var isAd: Boolean?, @NotNull @Ignore var tags: List<ImgurGalleryTag>, var inMostViral: Boolean?, @NotNull @Ignore var images: List<ImgurGalleryImage> ) 

After

 @Entity class ImgurGalleryPost ( @NotNull @PrimaryKey var id: String, var title: String?, var description: String?, var datetime: Int?, var cover: String?, var coverWidth: Int?, var coverHeight: Int?, var accountUrl: String?, var accountId: Int?, var privacy: String?, var layout: String?, var views: Int?, var link: String?, var ups: Int?, var downs: Int?, var points: Int?, var score: Int?, var isAlbum: Boolean?, var vote: Boolean?, var favorite: Boolean?, var nsfw: Boolean?, var section: String?, var commentCount: Int?, var favoriteCount: Int?, var topic: String?, var topicId: Int?, var imagesCount: Int?, var inGallery: Boolean?, var isAd: Boolean?, @NotNull @Ignore var tags: List<ImgurGalleryTag>, var inMostViral: Boolean?, @NotNull @Ignore var images: List<ImgurGalleryImage> ) 

This is really strange, but I doubt this is a problem with the Android Studio cache, because when I return to the data class error reappears. This seems to be some kind of issue with collection fields. I checked the constructor in the generated class and it looked fine, I don’t know why the assembly failed even when the constructor was generated correctly:

 public ImgurGalleryPost(@org.jetbrains.annotations.NotNull() java.lang.String id, @org.jetbrains.annotations.Nullable() java.lang.String title, @org.jetbrains.annotations.Nullable() java.lang.String description, @org.jetbrains.annotations.Nullable() java.lang.Integer datetime, @org.jetbrains.annotations.Nullable() java.lang.String cover, @org.jetbrains.annotations.Nullable() java.lang.Integer coverWidth, @org.jetbrains.annotations.Nullable() java.lang.Integer coverHeight, @org.jetbrains.annotations.Nullable() java.lang.String accountUrl, @org.jetbrains.annotations.Nullable() java.lang.Integer accountId, @org.jetbrains.annotations.Nullable() java.lang.String privacy, @org.jetbrains.annotations.Nullable() java.lang.String layout, @org.jetbrains.annotations.Nullable() java.lang.Integer views, @org.jetbrains.annotations.Nullable() java.lang.String link, @org.jetbrains.annotations.Nullable() java.lang.Integer ups, @org.jetbrains.annotations.Nullable() java.lang.Integer downs, @org.jetbrains.annotations.Nullable() java.lang.Integer points, @org.jetbrains.annotations.Nullable() java.lang.Integer score, @org.jetbrains.annotations.Nullable() java.lang.Boolean isAlbum, @org.jetbrains.annotations.Nullable() java.lang.Boolean vote, @org.jetbrains.annotations.Nullable() java.lang.Boolean favorite, @org.jetbrains.annotations.Nullable() java.lang.Boolean nsfw, @org.jetbrains.annotations.Nullable() java.lang.String section, @org.jetbrains.annotations.Nullable() java.lang.Integer commentCount, @org.jetbrains.annotations.Nullable() java.lang.Integer favoriteCount, @org.jetbrains.annotations.Nullable() java.lang.String topic, @org.jetbrains.annotations.Nullable() java.lang.Integer topicId, @org.jetbrains.annotations.Nullable() java.lang.Integer imagesCount, @org.jetbrains.annotations.Nullable() java.lang.Boolean inGallery, @org.jetbrains.annotations.Nullable() java.lang.Boolean isAd, @org.jetbrains.annotations.NotNull() java.util.List<com.kimboo.core.model.ImgurGalleryTag> tags, @org.jetbrains.annotations.Nullable() java.lang.Boolean inMostViral, @org.jetbrains.annotations.NotNull() java.util.List<com.kimboo.core.model.ImgurGalleryImage> images) { super(); } 

If someone can find a way to fix this without switching from a data class to a class please feel free to comment below.

0
source share

The error is caused by the originally set variables in the constructor. If you have only one constructor, change your code to this one.

 @Entity data class Site( val name: String, val url: String, @PrimaryKey(autoGenerate = true) val id: Long) 

If you also need an empty constructor, then you should do so

 @Entity data class Site() { constructor(name: String, url: String): this() { this.name = name this.url = url } var name: String = "", var url: String = "", @PrimaryKey(autoGenerate = true) var id: Long = 0 } 
0
source share
 @Entity data class Site @JvmOverloads constructor( @ColumnInfo(name = "name") var name: String = "", @ColumnInfo(name = "url") var url: String = "", @PrimaryKey(autoGenerate = true) var id: Long = 0) 

The constant model class for the site. To compile with Room, we could use @JvmOverloads to handle multiple constructors.

0
source share

All Articles