https://kotlinlang.org logo
Title
p

Poohrang

12/28/2020, 3:14 AM
Hey guys, I have a server for shopping mall. and I've created a database and some tables. When user presses like button in client side, Ktor server will increase by one interestCnt with SQL update query. But when I was trying to inrecase value of table, it doesn't work as below. (attached image1)
transaction {
    UsedItems.update({
        (UsedItems.e_mail eq receive.userEmail).and(UsedItems.id eq receive.usedItemId)}) {
        it[interestCnt] = interestCnt + 1 // occurs error
    }
}
interestCnt value had defined in UsedItems table as below (attached image2)
val interestCnt : Column<Int> = integer("interest_cnt").default(0)
When I tried to put just value(not increasing), It works properly (attached image3)
transaction {
    UsedItems.update({
        (UsedItems.e_mail eq receive.userEmail).and(UsedItems.id eq receive.usedItemId)}) {
        it[interestCnt] = 1
    }
}
I reffered the following code in exposed github documentation. If you want to update column value with some expression like increment use 
update
 function or setter:
StarWarsFilms.update({ StarWarsFilms.sequelId eq 8 }) {
    with(SqlExpressionBuilder) {
       it.update(StarWarsFilms.sequelId, StarWarsFilms.sequelId + 1)
       // or 
       it[StarWarsFilms.sequelId] = StarWarsFilms.sequelId + 1
    }
}
My expectation is to be worked like sample code. but not Is there any idea to solve this problem? Thanks in advance.
d

dave08

12/28/2020, 3:51 AM
// you mean this,no?
        it[interestCnt] = it[interestCnt] + 1
interestCnt itself is of type Column, so it cant be added to an Int
p

Poohrang

12/28/2020, 3:53 AM
@dave08 I already tried above code But i met the same result. 😞 I've uploaded attachment.
d

dave08

12/28/2020, 6:13 AM
Do you have an interestCnt in some outer context, like your fun params?
If so, you would need it[this.interestCnt]
Or it[UsedItems.interestCnt]
Also maybe += is overrided to work for such a case?
p

Poohrang

12/28/2020, 7:48 AM
it occurs still error even though above both guides are used.
I've just fixed it by adding following code.
with(SqlExpressionBuilder) {
  // Increment count
}
I missed it. But I'm still curious about that why does it need to increase count? Thanks for your support