【问题标题】:Scala projections in Slick for only one columnSlick 中的 Scala 投影仅用于一列
【发布时间】:2013-06-19 03:07:26
【问题描述】:

我正在关注 Slick documentation example for autoincrementing fields,但在创建 mapped projection 时遇到了问题……嗯,只有一列。

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   
}

错误是“值 不是 scala.slick.lifted.Column[String] 的成员”

我还认为这样设计我的架构会更有效:

case class UserRole(role: String)

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

}

但后来我也开始遇到与上述相同的错误。 "value 不是 scala.slick.lifted.Column[String] 的成员"

我到底在做什么?我只是没有projection 因为我只有一列吗?如果是这样,我应该怎么做

【问题讨论】:

    标签: scala projection slick


    【解决方案1】:

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

    幸运的是,您的代码无需映射投影即可工作。只需省略&lt;&gt; 之后的所有内容,包括&lt;&gt;。请参阅scala slick method I can not understand so far 以获得对预测的详细解释。它包含您开始工作所需的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多