Hi, I'm trying to use where in the most simple way...
# exposed
d
Hi, I'm trying to use where in the most simple way:
Copy code
val condition = when {
    name != null ->
        Op.build { Users.name like "%name%" }
    country != null ->
        Op.build { Users.country like "%country%" }
    else -> null
}

Users.selectAll().where(condition)
This will result in: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public operator fun <T, R> DeepRecursiveFunction<TypeVariable(T), TypeVariable(R)>.invoke(value: TypeVariable(T)): TypeVariable(R) defined in kotlin
a
Likely because you can’t call
selectAll().where(null)
d
Copy code
public final var where: org.jetbrains.exposed.sql.Op<kotlin.Boolean>? /* compiled code */
    private final set(value: org.jetbrains.exposed.sql.Op<kotlin.Boolean>?)
But it will give the same error with selectAll().where { Users.name like "%name%" }
a
You’re invoking a function called
where
, which doesn’t have a nullable overload. So you’d run into that next. Anyway, it could be that you have an issue with name shadowing. Could you try to make a minimal repro?
Also, I’m not sure you want to match against the literal
name
in your query 😅
d
Its a typo, yep
So it looks like I'm using older version of exposed, updated to the latest one and there I have the null case problem which is manageable
Thanks
👍 1
c
Hi @Dimitar Kostov. Out of curiosity, what SQL exactly do you intend the
else
branch to generate? If all users should be returned when both name and country are null,
Op.TRUE
could be used in the final branch instead to generate
WHERE TRUE
.
1
d
Hi, the issue here was me as expected. First we're using older version of exposed, second I was reading through the docs for conditional where, but adapted it poorly - the example was
val query = condition?.let { StarWarsFilms.selectAll().where(condition) } ?: StarWarsFilms.selectAll()
But I tried to use it directly
c
Ah I get it. Glad it was cleared up for you. As you're going through the docs, by the way, if you come across any confusing or missing areas that could be improved, please don't hesitate to open a ticket on YouTrack with your feedback.
👍 1