Slick 2.0 - update two or more columns

*爱你&永不变心* 提交于 2019-12-25 04:29:28

问题


I know I can update two columns in Slick 2.0 with:

val q = for (user <- Users if user.id === id) yield (user.name, user.city)
q.update((newName, newCity))

But I've seen something like this working as well which is IMO much nicer syntax:

Users.filter(_.id === id).map(u => u.name ~ u.city).update(newName, newCity)

This gives me the following error:

value ~ is not a member of shortcut.db.Tables.profile.simple.Column

I have imported PostgresDriver.simple._ and I just can't figure out why. I also use the code generator.

Thanks in advance!


回答1:


That is because the ~ method was moved out of the normal import scope for Slick 2.0. You can either use a tuple, just as is:

Users.filter(_.id === id).map(u => (u.name, u.city)).update((newName, newCity))

or import the necessary TupleMethods object:

import scala.slick.util.TupleMethods._
Users.filter(_.id === id).map(u => u.name ~ u.city).update((newName, newCity))



回答2:


In addition to Sean Vieira's answer:

for (user <- Users if user.id === id) yield (user.name, user.city)

is just syntactic sugar for

Users.filter(_.id === id).map(u => (u.name, u.city))

and that's not Slick-specific, it's the Scala compiler that desugars this for Scala-collections, for Slick, for anything.



来源:https://stackoverflow.com/questions/24174839/slick-2-0-update-two-or-more-columns

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