簡體   English   中英

value~不是slick.lifted.Rep [Option [Int]]的成員

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

我有一個Scala編譯錯誤我一直無法找到任何信息。 我正在使用光滑的3.0,並得到編譯錯誤

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

我相信這個問題與我使用Option代表我的ID字段的方式有關。

我試過添加id.? 這個答案中建議的id字段,但我仍然以同樣的方式得到編譯錯誤。 光滑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
  }
}

我認為問題是在光滑3.0.0中沒有像以前那樣使用某些符號

看看這里有進一步的問題

在你的情況下,有問題的行將是這樣的,取決於你將要做什么,但這應該工作:

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

此外,您不需要implicits導入

你的選項[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,沒有使用Option,就像我們在評論中談話一樣,我認為這種方法更好

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
  }
}

field1 ~ field2構造實際上是在引擎蓋下構造一個(field1, field2)元組,所以@anquegi指出,只需改變你的*投影就可以直接使用元組。

或者,如果你想使用~來構造元組,你可以通過導入TupleMethods來取回它(因為~ 被移出 Slick 2.0 中的正常導入范圍 。):

import slick.util.TupleMethods._

另請參閱: Slick 2.0 - 更新兩列或更多列

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM