Scala 使用Slick更新记录时遇到问题
类和表定义如下所示:Scala 使用Slick更新记录时遇到问题,scala,slick,Scala,Slick,类和表定义如下所示: case class Group( id: Long = -1, id_parent: Long = -1, label: String = "", description: String = "") object Groups extends Table[Group]("GROUPS") { def id = column[Long]("ID", O.PrimaryKey, O.AutoInc) def id_parent = col
case class Group(
id: Long = -1,
id_parent: Long = -1,
label: String = "",
description: String = "")
object Groups extends Table[Group]("GROUPS") {
def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
def id_parent = column[Long]("ID_PARENT")
def label = column[String]("LABEL")
def description = column[String]("DESC")
def * = id ~ id_parent ~ label ~ design <> (Group, Group.unapply _)
def autoInc = id_parent ~ label ~ design returning id into {
case ((_, _, _), id) => id
}
}
但我无法使用for表达式获取更新:
val findGById = for {
id <- Parameters[Long]
g <- Groups; if g.id === id
} yield g
def updateGroupX(g: Group)(implicit session: Session) = findGById(g.id).update(g)
----------------------------------------------------------------------------^
Error: value update is not a member of scala.slick.jdbc.MutatingUnitInvoker[com.exp.Group]
val findGById=for{
id类型提供了update
方法。该类型的实例可以通过productQueryToUpdateInvoker
方法和/或tableQueryToUpdateInvoker
(在中找到)方法从Query
隐式创建,如果它们在范围内
现在,您的findById
方法的类型不是Query
,而是BasicQueryTemplate[Long,Group]
。查看文档,我无法从BasicQueryTemplate
(它是StatementInvoker
的子类型)中找到任何方法对于一个UpdateInvoker
,既不是隐式的也不是显式的。想想看,这对我来说有点意义,因为我理解查询模板(调用程序)是已经从抽象语法树(“code>query
)中“编译”过的东西而更新调用程序只能从抽象语法树(即Query
对象)中构建,因为它需要分析查询并提取其参数/列。至少目前看来,它是这样工作的
考虑到这一点,一个可能的解决方案将展开:
def findGById(id: Long) = for {
g <- Groups; if g.id === id
} yield g
def updateGroupX(g: Group)(implicit session: Session) = findGById(g.id).update(g)
def findGById(id:Long)=用于{
g参考
今天我坚持更新,这篇博文帮了我很大的忙。也参考了这篇博文下的第一条评论。这是一个很好的解释,解释了为什么它不起作用,但在我看来这是一个问题。BasicQueryTemplate不如Query丰富,这一事实似乎使缓存具有参数和sortBy或groupBy的查询变得不可能。
def findGById(id: Long) = for {
g <- Groups; if g.id === id
} yield g
def updateGroupX(g: Group)(implicit session: Session) = findGById(g.id).update(g)