Home > Enterprise >  Android studio don't let me use primaryKeys for Room
Android studio don't let me use primaryKeys for Room

Time:09-25

In order to set uniqueness for the entities saved, I set primaryKeys like this:

@Entity(primaryKeys = ["name", "slidingDoorType"])
data class ParameterGroup(override var name: String, override var parameterIds: HashSet<Int>, var userCreated: Boolean, @PrimaryKey var slidingDoorType: String? = ""): FilterGroup, Parcelable {
    override var selected: Boolean = true
}

rather than using the standard @PrimaryKey. This however produces an error when trying to build the app:

Execution failed for task ':commonmodel:kaptDebugKotlin'. A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException (no error message)

Not too familiar with room to know what I'm doing wrong. But as soon as I revert to the old way of using @PrimaryKey I can build and run the app...

CodePudding user response:

Your problem is @PrimaryKey var slidingDoorType: String? = "" declaration. the issue is that you can't have nullable fields as primary keys.

In order to fix this change the type to non-nullable and remove the @PrimaryKey because you have already included this field using the primaryKeys property of @Entity.

var slidingDoorType: String = ""
  • Related