Skip to content

Commit

Permalink
fix: EXPOSED-371 Fix incorrect table reference passed to EntityID ins…
Browse files Browse the repository at this point in the history
…tance when using value-based utility functions (#2074)

* fix: push failing scenario

* fix: fix incorrect table reference passed to EntityID instance when using value-based utility functions
  • Loading branch information
dzikoysk committed May 5, 2024
1 parent d48e772 commit 1682990
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Expand Up @@ -36,7 +36,7 @@ abstract class UpdateBuilder<out T>(type: StatementType, targets: List<Table>) :
@Suppress("UNCHECKED_CAST")
@JvmName("setWithEntityIdValue")
operator fun <S : Comparable<S>> set(column: Column<EntityID<S>>, value: S) {
val entityId: EntityID<S> = EntityID(value, column.table as IdTable<S>)
val entityId: EntityID<S> = EntityID(value, (column.foreignKey?.targetTable ?: column.table) as IdTable<S>)
column.columnType.validateValueBeforeUpdate(entityId)
values[column] = entityId
}
Expand All @@ -47,7 +47,7 @@ abstract class UpdateBuilder<out T>(type: StatementType, targets: List<Table>) :
require(column.columnType.nullable || value != null) {
"Trying to set null to not nullable column $column"
}
val entityId: EntityID<S>? = value?.let { EntityID(it, column.table as IdTable<S>) }
val entityId: EntityID<S>? = value?.let { EntityID(it, (column.foreignKey?.targetTable ?: column.table) as IdTable<S>) }
column.columnType.validateValueBeforeUpdate(entityId)
values[column] = entityId
}
Expand Down
Expand Up @@ -280,6 +280,23 @@ class InsertTests : DatabaseTestsBase() {
}
}

@Test fun testInsertWithForeignId() {
val idTable = object : IntIdTable("idTable") {}
val standardTable = object : Table("standardTable") {
val externalId = reference("externalId", idTable.id)
}
withTables(idTable, standardTable) {
val id1 = idTable.insertAndGetId {}

standardTable.insert {
it[externalId] = id1.value
}

val allRecords = standardTable.selectAll().map { it[standardTable.externalId] }
assertTrue(allRecords == listOf(id1))
}
}

@Test fun testInsertWithExpression() {
val tbl = object : IntIdTable("testInsert") {
val nullableInt = integer("nullableIntCol").nullable()
Expand Down

0 comments on commit 1682990

Please sign in to comment.