6

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

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

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

我已尝试按照此答案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
  }
}
4

2 回答 2

4

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

这里查看更多问题

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

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

你也不需要隐式导入

您的 option[Int] 参数也存在问题:也许这应该更好:

import slick.driver.H2Driver.api._


object SlickStackOverflow 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 SlickStackOverflow 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
  }
}
于 2015-07-06T14:56:28.900 回答
2

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

或者,如果你想用来~构造元组,你可以通过导入来取回它TupleMethods(因为在 Slick 2.0 中~ 移出了正常的导入范围。):

import slick.util.TupleMethods._

另请参阅:Slick 2.0 - 更新两列或更多列

于 2015-07-06T15:04:14.977 回答