Dominik Sandjaja
02/21/2024, 1:51 PM.upsert(){}
) functionality in exposed, is there a way to exclude a column from being updated when an update is done? Or is the only way to do so by defining a dedicated onUpdate
statement? I was hoping of something like onUpdateExclude: Array<Column<*>>
.
My usecase: I have tables with a column like
val createdAt = datetime("created_at").defaultExpression(CurrentDateTime)
which works fine for inserts, but I clearly don't want to update this column when I UPDATE
the column.Chantal Loncle
02/21/2024, 3:09 PMonUpdate
is currently the only option.
If the difference only involves the exclusion of some columns (so all other column + value pairings stay the same), there is already a YouTrack issue requesting a way to avoid duplicating most of the insert block.
Please consider upvoting that issue and including your use case above.amarcantoni
02/27/2024, 5:04 PMbatchUpsert
for similar use cases?
I would be fine with a maybe "ugly" solution that requires some code duplication, I just can't find a way to update the columns in the `onUpdate`statement with the correct values from the batch...
Thank you lots!Chantal Loncle
02/27/2024, 6:44 PMonUpdateExclude
, also available to batchUpsert()
. This can be used to update columns with the right batch values instead of duplicating from the insert block:
object Words : Table("words") {
val word = varchar("name", 64).uniqueIndex()
val letters = integer("letters")
val added = datetime("added").defaultExpression(CurrentDateTime)
}
// on conflict, do not update 'added' but use insert value for 'letters'
Words.batchUpsert(
listOfWords,
onUpdateExclude = listOf(Words.added)
) { word ->
this[Words.word] = word
this[Words.letters] = word.length
}
This Wiki section also has a new example for using onUpdateExclude
in the reverse way (to only update specific columns using the insert batch values).amarcantoni
02/27/2024, 7:31 PM