Search code examples
scalaslickslick-3.0

How to update a row omitting a column in Slick 3.x?


I have the following code in Slick that updates an object user:

val users = TableQuery[UserDB]
val action = users.filter(_.id === user.id).update(user)
val future = db.run(action)
val result = Await.result(future, Duration.Inf)

But there's a field in the user object (password) that I don't want to update. How to omit it?


Solution

  • You should select columns using a map operation before an update operation:

    case class User(name: String, age: Int, password: String, id: Int)
    
    val updatedUser = User("Pawel", 25, "topsecret", 123)
    val users = TableQuery[UserDB]
    val action = users.filter(_.id === updatedUser.id).map(user => 
      (user.name, user.age)
    ).update(
      (updatedUser.name, updatedUser.age)
    )
    val future = db.run(action)
    val result = Await.result(future, Duration.Inf)