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

2019-07-11 07:44发布

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条回答
女痞
2楼-- · 2019-07-11 08:14

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)
查看更多
登录 后发表回答