thedarkpassenger
thedarkpassenger

Reputation: 7348

Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type

I have a DB which has a custom data type FollowEntityType as a column.

@Entity(primaryKeys = arrayOf("id", "type"), tableName = "follow_info")
data class FollowInfoEntity(
        @ColumnInfo(name = "id") var id: String,
        @ColumnInfo(name = "type") var type: FollowEntityType,
)

Since it is a custom data type, I have defined a type converter.

class FollowDatabaseTypeConverter {

    @TypeConverter
    fun toFollowEntity(entityType: String?): FollowEntityType? {
        return FollowEntityType.from(entityType ?: Constants.EMPTY_STRING)
    }

    @TypeConverter
    fun toString(entityType: FollowEntityType?): String? {
        return entityType?.name
    }
}

This works fine and I am able to store/retrieve values in the DB. However, in one of the queries, the build fails.

This is the query.

@Query("select * from follow_info where type in (:entityTypeList)")
fun getFollowedEntitiesByType(entityTypeList: List<FollowEntityType>) : List<FollowInfoEntity>

The build fails with the following error.

Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
    java.util.List<? extends FollowEntityType> entityTypeList, @org.jetbrains.annotations.NotNull()

The error is for entityTypeList field, and according to the error, this type should be one of the columns in the DB. I already have FollowEntityType as one of the column types. I don't understand why it is failing. Please help me out as I am not finding any solution to solve this problem.

Upvotes: 21

Views: 18521

Answers (9)

Devendra Bhatta
Devendra Bhatta

Reputation: 1

[ksp] D:/code/ToDoApp/app/src/main/java/com/devdeveloper/todoapp/data/ToDoDao.kt:33: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.

i am using kotlin 2.0 room version 2.6.1 and id("com.google.devtools.ksp") version "2.0.0-1.0.22" apply false

Upvotes: 0

Alijan Mirzayev
Alijan Mirzayev

Reputation: 1

Kotlin version 1.9.0 & Room version 2.6.2, resolved it

Upvotes: 0

Sam Lu
Sam Lu

Reputation: 3506

I also see the same issue with Kotlin 1.7.10 + Room 2.4.2. No problem with Kotlin 1.6.21 + Room 2.4.2.

Kotlin 1.7.10 + Room 2.5.0-alpha02 is also okay, so I guess we have to wait for Room 2.5.0 formal release before using Kotlin 1.7.x

2022/8/4: After the AndroidX team released a new version of ROOM. Kotlin 1.7.10 + Room 2.4.3 is okay now.

2022/10/7: If you are using Kotlin 1.9.0 or greater version, compile with Room 2.5.2 won't work again. You have to use Kotlin 1.9.0 + Room 2.6.0-rc01(or greater version). Changing to use KSP instead of KAPT is also recommended by Google. I am using Kotlin 1.9.0 + KSP 1.9.0-1.0.13 + Room 2.5.2

Upvotes: 26

Jemshit
Jemshit

Reputation: 10038

Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this. kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);

For Kotlin 1.9.0 and Room 2.5.2, i removed kapt plugin and replaced it with ksp following this doc. ksp and Kotlin have compatible versioning.

Upvotes: 3

Vivek Pratap Singh
Vivek Pratap Singh

Reputation: 1662

Kindly update the Kotlin and Room DB version, It worked for me I changed to Kotlin 1.6.21 + Room 2.4.2. and it worked.

Upvotes: 1

madabrowski
madabrowski

Reputation: 1561

I'm using room 2.4.2 with a android docs copy pasted documentation

It seems like this is a manifestation of room <-> kotlin version mismatch

I do get

error: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
    kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);

When i use

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0

when i use

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'

i don't get this error

Upvotes: 0

SUBODH KUMAR Gupta
SUBODH KUMAR Gupta

Reputation: 99

Remove the suspend from the method when you use room query annotation. Like

Old

@Query("SELECT * FROM userSession where isSync=0 ORDER BY createdOnInMills")
suspend fun getUnSyncSession(): List<SessionModel>

remove Suspend

New

@Query("SELECT * FROM userSession where isSync=0 ORDER BY createdOnInMills")
fun getUnSyncSession(): List<SessionModel>

Upvotes: 9

Ahmet B.
Ahmet B.

Reputation: 1674

I got this error when I tried executing this Query in Dao interface;

@Query("DELETE FROM my_table WHERE customModel = :customModel")
fun deleteData(customModel: CustomModel)

Fix it by adding @TypeConverters(MyConverter::class) as below;

@TypeConverters(MyConverter::class)
@Query("DELETE FROM my_table WHERE customModel = :customModel")
fun deleteData(customModel: CustomModel)

Upvotes: 5

Vasudev
Vasudev

Reputation: 1996

This error occurred for me when I updated the kotlin library version without updating the room database version.

I was using Kotlin 1.5.10 and Room version 2.2.5 that caused the problem. Changing Room to 2.3.0 fixed the error.

Upvotes: 41

Related Questions