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

2019-07-11 08:26发布

问题:

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?

回答1:

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)