如何避免Slick异常“对于UPDATE语句的查询必须仅选择表列". [英] How to avoid Slick exception "A query for an UPDATE statement must select table columns only"

查看:198
本文介绍了如何避免Slick异常“对于UPDATE语句的查询必须仅选择表列".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下案例类(请注意最后一个字段中的选项)

I have the following case class (note the Option in the last field)

case class BranchVO(sk: Int, name: String, bankSk: Int, bankName: Option[String])

以及以下用于让Slick访问数据库的类(请注意*方法中的None,因为case类具有一个附加的last字段):

And the following class to have Slick access the database (Note the None in the * method, as the case class has an additional last field):

class BranchDB(tag: Tag) extends Table[BranchVO](tag, "branches") {

  def sk: Rep[Int] = column[Int]("sk", O.PrimaryKey, O.AutoInc)
  def name: Rep[String] = column[String]("name")
  def bankSk: Rep[Int] = column[Int]("bank_sk")

  def * = (sk, name, bankSk, None) <> (BranchVO.tupled, BranchVO.unapply)
}

当我尝试在Slick中更新一行时,得到slick.SlickException: A query for an UPDATE statement must select table columns only -- Unsupported shape: ProductNode

When I attempt to update a row in Slick, I get slick.SlickException: A query for an UPDATE statement must select table columns only -- Unsupported shape: ProductNode

我需要案例类具有最后一个字段.我可以解决这个问题,创建另一个没有最后一个字段的case类,但是我试图避免这种情况,是否有解决方法?

I need the case class to have the last field. I could solve this problem creating yet another case class without the last field, but I'm trying to avoid that, is there a workaround?

推荐答案

这是Odomontois的解决方案(

Here is the solution by Odomontois (How can I omit case class fields in a slick table mapping?):

type Data = (Long, String, String, Option[String])

def constructUser: Data => User = {
  case (id, username, passwordHash, email) => User(id, username, passwordHash, email)

}
def extractUser: PartialFunction[User, Data] = {
  case User(id, username, passwordHash, email, _, _) =>
    (id, username, passwordHash, email)
}

def * = (id, username, passwordHash, email) <> (constructUser, extractUser.lift)

这篇关于如何避免Slick异常“对于UPDATE语句的查询必须仅选择表列".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆