I am trying to make a request to my database using Exposed, everywhere I'am looking it says that i need to put the condition in the function body like so:
override suspend fun getCarModel(carModelId: Int): CarModel? {
return transaction {
addLogger(StdOutSqlLogger)
CarModels
.select {CarModels.id eq carModelId}
.mapNotNull { row ->
CarModel(
id = row[CarModels.id],
brand = row[CarModels.brand],
model = row[CarModels.model],
year = row[CarModels.year]
)
}.singleOrNull()
}
}
Doing this way I get an error of:
Type mismatch.
Required:
List<Expression<*>>
Found:
() → Op
I've also tried putting the condition in the function arguments
.select(CarModels.id eq carModelId)
or .select{(CarModels.id eq carModelId)} but this way I get:
com.carspotter.data.table.CarModels.id is not in record set java.lang.IllegalStateException: com.carspotter.data.table.CarModels.id is not in record set.
This is my table declaration:
object CarModels : Table("car_models") {
val id = integer("id").autoIncrement()
val brand = varchar("brand", 50)
val model = varchar("model", 50)
val year = integer("year").nullable()
override val primaryKey = PrimaryKey(id)
init {
uniqueIndex(brand, model, year)
}
}
My question is what am I doing wrong that the first approach doesn't work? Also, I am using Exposed version 0.60.0 for core, jdbc and dao.
I am trying to make a request to my database using Exposed, everywhere I'am looking it says that i need to put the condition in the function body like so:
override suspend fun getCarModel(carModelId: Int): CarModel? {
return transaction {
addLogger(StdOutSqlLogger)
CarModels
.select {CarModels.id eq carModelId}
.mapNotNull { row ->
CarModel(
id = row[CarModels.id],
brand = row[CarModels.brand],
model = row[CarModels.model],
year = row[CarModels.year]
)
}.singleOrNull()
}
}
Doing this way I get an error of:
Type mismatch.
Required:
List<Expression<*>>
Found:
() → Op
I've also tried putting the condition in the function arguments
.select(CarModels.id eq carModelId)
or .select{(CarModels.id eq carModelId)} but this way I get:
com.carspotter.data.table.CarModels.id is not in record set java.lang.IllegalStateException: com.carspotter.data.table.CarModels.id is not in record set.
This is my table declaration:
object CarModels : Table("car_models") {
val id = integer("id").autoIncrement()
val brand = varchar("brand", 50)
val model = varchar("model", 50)
val year = integer("year").nullable()
override val primaryKey = PrimaryKey(id)
init {
uniqueIndex(brand, model, year)
}
}
My question is what am I doing wrong that the first approach doesn't work? Also, I am using Exposed version 0.60.0 for core, jdbc and dao.
Share Improve this question edited Mar 28 at 16:00 Andrei Rusu asked Mar 27 at 20:34 Andrei RusuAndrei Rusu 193 bronze badges 2- Can you show the SQL equivalent SELECT query you are trying to code in Exposed? – k314159 Commented Mar 31 at 12:19
- This the query I am trying to execute: SELECT car_models.id, car_models.brand, car_models.model, car_models.year FROM car_models WHERE car_models.id = 1 – Andrei Rusu Commented Apr 1 at 11:13
1 Answer
Reset to default 0The message explains what is causing the error:
Type mismatch.
Required: List<Expression<*>>
Found: () -> Op
The select
function, just like the SQL SELECT statement, takes a list of columns, but you are passing a lambda to the select
function (hence Found: () -> Op
). You need to pass the condition to the where
function instead:
CarModels
.select(listOf(CarModels.id, CarModels.brand, CarModels.model, CarModels.year))
.where { CarModels.id eq carModelId }