Skip to content

Commit

Permalink
fix: EXPOSED-602 Column name that includes table name is aliased with…
Browse files Browse the repository at this point in the history
… upserts (#2287)

Dialects that use MERGE for upserts automatically have their update values aliased.
Since version 0.54.0, update values that should be the same as insert values are
wrapped as a special expression. This means they are processed like any expression
and any potential inclusion of the table name is replaced with alias 'T'. This
causes invalid identifiers if the actual column to update uses an identifier
that includes the table name. Restricting the case for string replacement
fixes this.
  • Loading branch information
bog-walk authored Oct 29, 2024
1 parent ca1db70 commit c5c5c7b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ abstract class FunctionProvider {
append("T.${transaction.identity(columnToUpdate)}=")
when (updateExpression) {
is QueryParameter<*>, !is Expression<*> -> registerArgument(columnToUpdate.columnType, updateExpression)
else -> append(updateExpression.toString().replace(tableIdentifier, "T"))
else -> append(updateExpression.toString().replace("$tableIdentifier.", "T."))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,24 @@ class UpsertTests : DatabaseTestsBase() {
}
}

@Test
fun testUpsertWhenColumnNameIncludesTableName() {
val tester = object : Table("my_table") {
val myTableId = integer("my_table_id")
val myTableValue = varchar("my_table_value", 100)
override val primaryKey = PrimaryKey(myTableId)
}

withTables(excludeSettings = TestDB.ALL_H2_V1, tester) {
tester.upsert {
it[myTableId] = 1
it[myTableValue] = "Hello"
}

assertEquals("Hello", tester.selectAll().single()[tester.myTableValue])
}
}

private object AutoIncTable : Table("auto_inc_table") {
val id = integer("id").autoIncrement()
val name = varchar("name", 64)
Expand Down

0 comments on commit c5c5c7b

Please sign in to comment.