![](/img/trans.png)
[英]value isEmpty is not a member of slick.lifted.Rep[Option[java.time.LocalDateTime]]
[英]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.