-->

How to avoid Slick exception “A query for an UPDAT

2019-09-12 07:11发布

问题:

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)