Scala projections in Slick for only one column

五迷三道 提交于 2019-12-01 15:45:59

问题


I'm following the Slick documentation example for autoincrementing fields and I'm having trouble creating a mapped projection that ... well, only has one column.

case class UserRole(id: Option[Int], role: String)

object UserRoles extends Table[UserRole]("userRole") {
  def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
  def role = column[String]("ROLE")
  // ...
  def * = id.? ~ role <> (UserRole, UserRole.unapply _)
      // NEXT LINE ERRORS OUT
  def forInsert = role <> ({t => UserRole(None, t._1)}, {(r: UserRole) => Some((r.role))}) returning id   
}

The error is "value <> is not a member of scala.slick.lifted.Column[String]"

I also thought it'd be more efficient to design my schema like so:

case class UserRole(role: String)

object UserRoles extends Table[UserRole]("userRole") {
  def role = column[Int]("ROLE", O.PrimaryKey)
  // ...
  def * = role <> (UserRole, UserRole.unapply _)

}

But then I start getting the same error as above, too. "value <> is not a member of scala.slick.lifted.Column[String]"

What am I really doing? Do I just not have a projection anymore because I only have one column? If so, what should I be doing?


回答1:


This is a known issue with Slick; mapped projections do not work with a single column. See https://github.com/slick/slick/issues/40

Luckily, you don't need a mapped projection for your code to work. Just omit everything after and including the <>. See scala slick method I can not understand so far for a great explanation of projections. It includes the information you need to get going.



来源:https://stackoverflow.com/questions/17244478/scala-projections-in-slick-for-only-one-column

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