Search code examples
kotlinkotlin-exposed

jetbrains exposed - select based on nullable reference column


I'm trying to select the rows from a table based on a nullable referenced column. If you replace the reference with just a standard integer column and keep it nullable, it handles the eq just fine. I've also tried replacing the reference with optReference, but that didn't make a difference.

The error is given by the compiler.

None of the following functions can be called with the arguments supplied.
Expression<in EntityID<Int>?>.eq(Expression<in EntityID<Int>?>)   where T = EntityID<Int>?, S1 = EntityID<Int>?, S2 = EntityID<Int>? for    infix fun <T, S1 : T?, S2 : T?> Expression<in S1>.eq(other: Expression<in S2>): Op<Boolean> defined in org.jetbrains.exposed.sql.SqlExpressionBuilder
ExpressionWithColumnType<T>.eq(T)   where T cannot be inferred for    infix fun <T> ExpressionWithColumnType<T>.eq(t: T): Op<Boolean> defined in org.jetbrains.exposed.sql.SqlExpressionBuilder
ExpressionWithColumnType<EntityID<Int>>.eq(Int?)   where T = Int for    infix fun <T : Comparable<T>> ExpressionWithColumnType<EntityID<T>>.eq(t: T?): Op<Boolean> defined in org.jetbrains.exposed.sql.SqlExpressionBuilder

A basic working example showing the error given by Intellij.

object A : IntIdTable("a") {
    val n = varchar("n", 255)
    val x = reference("x", B).nullable()
}

object B : IntIdTable("b") {
    val i = varchar("m", 255)
    val y = integer("y")
}

fun main() {
    connectToDatabase()

    transaction {
        SchemaUtils.createMissingTablesAndColumns(A, B)

        A.select { A.x eq 1 }
    }
}

The equivalent sql I'm want it to run is:

select * from test.a as a where a.x = 1;

Solution

  • A.x is not a Column<Int>, its type is actually Column<EntityID<Int>>.

    It looks like you need to write the query as

    A.select { A.x eq EntityID(1, B) }