How to avoid Slick exception “A query for an UPDATE statement must select table columns only”

余生颓废 提交于 2019-12-12 05:54:17

问题


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])

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)
}

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

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?


回答1:


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)


来源:https://stackoverflow.com/questions/35454773/how-to-avoid-slick-exception-a-query-for-an-update-statement-must-select-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!