Using Exposed 0.17.3, Kotlin 1.3.50, and PostgresQL JDBC 42.2.6, I am unable to create new IdTable-derived entity rows which reference other IdTable-derived entity IDs.
Given a simple schema:
object Users : IntIdTable() {
val name = varchar("name", 10)
}
object Messages : IntIdTable() {
val owner = reference("owner_id", Users.id)
val content = text("content")
}
And given a simple insert of a new Messages row:
transaction {
val id = Users.insertAndGetId {
it[name] = "Example"
}
Messages.insert {
it[owner] = id // type is EntityId<Int>
it[content] = "Hello world!"
}
}
An exception is thrown when the user ID is bound to the prepared statement for the Messages insert:
org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.jetbrains.exposed.dao.EntityID. Use setObject() with an explicit Types value to specify the type to use.
at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:955)
at org.jetbrains.exposed.sql.IColumnType$DefaultImpls.setParameter(ColumnType.kt:56)
at org.jetbrains.exposed.sql.ColumnType.setParameter(ColumnType.kt:60)
at org.jetbrains.exposed.sql.statements.StatementKt.fillParameters(Statement.kt:116)
at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed(Statement.kt:51)
...
Stepping through the code in a debugger, the column type is INTEGER but the object type is still EntityId rather than Int (the Int value has not been unwrapped).
Is this the expected behavior? Is the example above representative of the expected code for this use case?
A sample project with DAO and DSL test cases is available at plm/exposed-entityid-example.
@plm, thank you for a report. As a workaround you could replace reference("owner_id", Users.id) with reference("owner_id", Users)
@Tapac, I tested the workaround in my sample project and both the DAO and DSL test cases pass.
@Tapac, do you know of any workaround for this when the referenced table has a composite PK? Can't use IdTables in that case.
@kspar , never try it.
Will look at that issue in the near future.
Fixed in 0.21.1