[英]value ~ is not a member of slick.lifted.Rep[Option[Int]]
I have a Scala compile error I have been unable to find any information for. 我有一个Scala编译错误我一直无法找到任何信息。 I am using slick 3.0 and am getting a compile error of
我正在使用光滑的3.0,并得到编译错误
value ~ is not a member of slick.lifted.Rep[Option[Int]]
I believe the issue relates to the way I am using an Option to represent my ID field. 我相信这个问题与我使用Option代表我的ID字段的方式有关。
I have tried adding id.?
我试过添加
id.?
to the id field as suggested in this answer but I still get a compilation error in the same vein. 到这个答案中建议的id字段,但我仍然以同样的方式得到编译错误。 Has anything change in slick 3.0?
光滑3.0有什么变化吗?
My code is as follows: 我的代码如下:
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
}
}
I think that the problem is that some symbols are not used like before in slick 3.0.0 我认为问题是在光滑3.0.0中没有像以前那样使用某些符号
Take a look here for further problems 看看这里有进一步的问题
in you case the problematic line will be something like this depending what are you going to do, but this should work: 在你的情况下,有问题的行将是这样的,取决于你将要做什么,但这应该工作:
def * = (id, name, instructions, ingredients) <> ((Recipe.apply _).tupled, Recipe.unapply _) def * =(id,name,instructions,ingredients)<>((Recipe.apply _)。tupled,Recipe.unapply _)
Also you do not need the implicits import 此外,您不需要implicits导入
and there is also a problem with your option[Int] parameter: maybe this should be better: 你的选项[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
}
}
Or adding .? 或者添加。? to the field id, whithout using Option, like we talk in comments, I think that this aproach is better
对于字段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
}
}
The field1 ~ field2
construct is actually constructing a (field1, field2)
tuple under the hood, so as @anquegi points out, simply changing your *
projection to use a tuple directly will work. field1 ~ field2
构造实际上是在引擎盖下构造一个(field1, field2)
元组,所以@anquegi指出,只需改变你的*
投影就可以直接使用元组。
Alternatively, if you want to use ~
to construct the tuple you can get it back by importing TupleMethods
(as ~
was moved out of the normal import scope in Slick 2.0.): 或者,如果你想使用
~
来构造元组,你可以通过导入TupleMethods
来取回它(因为~
被移出 Slick 2.0 中的正常导入范围 。):
import slick.util.TupleMethods._
See also: Slick 2.0 - update two or more columns 另请参阅: Slick 2.0 - 更新两列或更多列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.