Skip to content

Commit

Permalink
fix: EXPOSED-27 Id is not in record set
Browse files Browse the repository at this point in the history
In the `setInternal` function in `ResultRow`, the expression `c` could not be found in the `fieldIndex` map because the column comparison fails because of a type mismatch between EntityIDColumnType and VarCharColumnType.
  • Loading branch information
joc-a committed Apr 25, 2023
1 parent 4341bef commit 4035170
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ class ResultRow(
}

private fun <T> setInternal(c: Expression<out T>, value: T) {
val index = fieldIndex[c] ?: error("$c is not in record set")
val index = fieldIndex[c]
?: ((c as? Column<*>)?.columnType as? EntityIDColumnType<*>)?.let { fieldIndex[it.idColumn] }
?: fieldIndex.keys.firstOrNull { exp ->
when (exp) {
// exp is Column<*> && exp.table is Alias<*> -> exp.table.delegate == c
is Column<*> -> (exp.columnType as? EntityIDColumnType<*>)?.idColumn == c
is ExpressionAlias<*> -> exp.delegate == c
else -> false
}
}?.let { fieldIndex[it] }
?: error("$c is not in record set")
data[index] = value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1319,4 +1319,28 @@ class EntityTests : DatabaseTestsBase() {
)
}
}

object RequestsTable : IdTable<String>() {
val requestId: Column<String> = varchar("requestId", 256)
override val primaryKey = PrimaryKey(requestId)
override val id: Column<EntityID<String>> = requestId.entityId()
}

class Request(id: EntityID<String>) : Entity<String>(id) {
companion object : EntityClass<String, Request>(RequestsTable)

var requestId by RequestsTable.requestId
}

@Test
fun testSelectFromStringIdTableWithPrimaryKeyByColumn() {
withTables(RequestsTable) {
Request.new {
requestId = "123"
}

val count = Request.all().count()
assertEquals(1, count)
}
}
}

0 comments on commit 4035170

Please sign in to comment.