值 ~ 不是 slick.lifted.Rep[Option[Int]] 的成员

value ~ is not a member of slick.lifted.Rep[Option[Int]]

我有一个 Scala 编译错误,我找不到任何相关信息。我正在使用 slick 3.0 并收到

的编译错误

value ~ is not a member of slick.lifted.Rep[Option[Int]]

我认为这个问题与我使用选项来表示我的 ID 字段的方式有关。

我已经尝试按照 this answer 中的建议将 id.? 添加到 id 字段,但我仍然遇到同样的编译错误。 slick 3.0 有什么变化吗?

我的代码如下:

import slick.driver.H2Driver.api._
import scala.concurrent.ExecutionContext.Implicits.global

case class Recipe(id: Option[Int] = None, name: String, instructions: String, ingredients: String)

object AddFixtures {

  class Recipes(tag: Tag) extends Table[Recipe](tag, "recipe") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def name = column[String]("name")
    def instructions = column[String]("instructions")
    def ingredients = column[String]("ingredients")

    def * = id ~ name ~ instructions ~ ingredients <> (Recipe, Recipe.unapply _)
  }

  val recipes = TableQuery[Recipes]

  val setup = DBIO.seq(
    recipes.schema.create,
    recipes += Recipe(None, "Chicken with Avocado", "Mix it up", "Chicken, Avocado")
  )

  def apply() = {
    val db = Database.forConfig("h2mem1")

    try db.run(setup)
    finally db.close
  }
}

我认为问题是一些符号在 slick 3.0.0 中没有像以前那样使用

查看here 以了解更多问题

在你的情况下,有问题的行将是这样的,这取决于你要做什么,但这应该有效:

def * = (id, name, instructions, ingredients) <> ((Recipe.apply _).tupled, Recipe.unapply _)

你也不需要隐式导入

而且你的option[Int]参数也有问题:也许这样应该更好:

import slick.driver.H2Driver.api._


object SlickWhosebug extends App {

}

case class Recipe(id: Option[Int] = None, name: String, instructions: String, ingredients: String)

object AddFixtures {

  class Recipes(tag: Tag) extends Table[Recipe](tag, "recipe") {
    def id = column[Option[Int]]("id", O.PrimaryKey, O.AutoInc)
    def name = column[String]("name")
    def instructions = column[String]("instructions")
    def ingredients = column[String]("ingredients")

    def * = (id, name, instructions, ingredients) <> ((Recipe.apply _).tupled, Recipe.unapply _)
  }

  val recipes = TableQuery[Recipes]

  val setup = DBIO.seq(
    recipes.schema.create,
    recipes += Recipe(None, "Chicken with Avocado", "Mix it up", "Chicken, Avocado")
  )

  def apply() = {
    val db = Database.forConfig("h2mem1")

    try db.run(setup)
    finally db.close
  }
}

或者添加.?到字段id,不使用选项,就像我们在评论中所说的那样,我认为这种方法更好

package org.example

import slick.driver.H2Driver.api._

object SlickWhosebug extends App {

}

case class Recipe(id: Option[Int], name: String, instructions: String, ingredients: String)

object AddFixtures {

  class Recipes(tag: Tag) extends Table[Recipe](tag, "recipe") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def name = column[String]("name")
    def instructions = column[String]("instructions")
    def ingredients = column[String]("ingredients")

    def * = (id.?, name, instructions, ingredients) <> ((Recipe.apply _).tupled, Recipe.unapply _)
  }

  val recipes = TableQuery[Recipes]

  val setup = DBIO.seq(
    recipes.schema.create,
    recipes += Recipe(None, "Chicken with Avocado", "Mix it up", "Chicken, Avocado")
  )

  def apply() = {
    val db = Database.forConfig("h2mem1")

    try db.run(setup)
    finally db.close
  }
}

field1 ~ field2 构造实际上是在幕后构造一个 (field1, field2) 元组,因此正如@anquegi 指出的那样,只需将 * 投影更改为直接使用元组即可。

或者,如果您想使用 ~ 构造元组,您可以通过在 Slick 2.0 中导入 TupleMethods (as ~ was moved out of the normal import scope 来取回它。):

import slick.util.TupleMethods._

另请参阅:Slick 2.0 - update two or more columns