Scala Slick. How to change a column value using Case

45 Views Asked by At
case class User(name: String, age: Int)

def changeNameQuery(): Query[UserTable, User, Seq] = {
  TableQuery[UserTable].map { user => 
    (Case If (user.name === "A") Then "B" Else "A",
     user.age
    )
  }
}

i get this error

[error]  found   : slick.lifted.Query[(slick.lifted.Rep[String], slick.lifted.Rep[Int]),(String, Int),Seq]
[error]  required: repositories.postgres.PostgresProfile.api.Query[repositories.postgres.UserTable,model.User,Seq]

Maybe there is some way to get a table object back from a tuple?

2

There are 2 best solutions below

1
Gastón Schabas On

At first sight it seems a typing problem. You are saying that you will return a Query[User Table, User, Seq], which means from the user table you are going to return a list of users. The error says that you are returning a tuple (String,Int) instead of User.

Instead of doing

(
  Case If (user.name === "A") Then "B" Else "A",
  user.age   
)

You need to return a User or change the return type of your method. It depends on what you need

1
Mehdi.t On

Have you considered using slick.jdbc.Database.run method?

val db: Database = Database.forConfig("myDatabase")
db.run(analyticsActivityTableQuery += value)

You can check and update the value of value.