Slick 中的 Scala 投影仅用于一列 [英] Scala projections in Slick for only one column

查看:49
本文介绍了Slick 中的 Scala 投影仅用于一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 自动递增字段的流畅文档示例 我在创建 映射投影 那......好吧,只有一列.

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.?~ 角色 <>(UserRole, UserRole.unapply _)//下一行错误输出def forInsert = 角色 <>({t => UserRole(None, t._1)}, {(r: UserRole) => Some((r.role))}) 返回 id}

错误是值 <> 不是 scala.slick.lifted.Column[String] 的成员"

我也认为像这样设计我的模式会更有效:

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

但随后我也开始收到与上述相同的错误.值 <> 不是 scala.slick.lifted.Column[String] 的成员"

我到底在做什么?我是否因为只有一列而不再有 projection ?如果是这样,我应该做什么?

解决方案

这是 Slick 的一个已知问题;映射投影不适用于单列.参见 https://github.com/slick/slick/issues/40>

幸运的是,您的代码不需要映射投影即可工作.只需省略 <> 之后的所有内容.参见 scala slick 方法我目前无法理解对预测的一个很好的解释.它包括您开始工作所需的信息.

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?

解决方案

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.

这篇关于Slick 中的 Scala 投影仅用于一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆