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

岁酱吖の 提交于 2019-12-24 00:35:34

问题


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)


来源:https://stackoverflow.com/questions/37567709/how-to-update-a-row-omitting-a-column-in-slick-3-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!