我有一个Scala编译错误我无法找到任何信息。我正在使用光滑的3.0并且收到
的编译错误 timer
我认为这个问题与我使用Option代表我的ID字段的方式有关。
我已尝试按this answer中的建议将value ~ is not a member of slick.lifted.Rep[Option[Int]]
添加到id字段,但我仍然以同样的方式得到编译错误。光滑3.0有什么变化吗?
我的代码如下:
id.?
答案 0 :(得分:3)
我认为问题是在光滑的3.0.0中没有像以前那样使用某些符号
查看here以了解更多问题
在你的情况下,有问题的行将是这样的,取决于你将要做什么,但这应该工作:
def * =(id,名称,说明,成分)<> ((Recipe.apply _)。tupled,Recipe.unapply _)
此外,您不需要implicits import
你的选项[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
}
}
答案 1 :(得分:2)
field1 ~ field2
构造实际上构建了一个(field1, field2)
元组,因此@anquegi指出,只需更改*
投影即可直接使用元组将起作用。
或者,如果您想使用~
来构建元组,可以通过导入TupleMethods
(在Slick 2.0中为~
was moved out of the normal import scope来取回它。):< / p>
import slick.util.TupleMethods._