使用连接查询后Scala光滑的更新表

时间:2017-09-17 02:15:44

标签: scala slick

我想更新表格,但需要根据特定条件选择行。以下代码编译良好但抛出运行时异常:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[SlickException: A query for an UPDATE statement must resolve to a comprehension with a single table -- Unsupported shape: Comprehension s2, Some(Apply Function =), None, ConstArray(), None, None, None, None]]

这是函数(目的是仅允许合格用户更新):

def updateRecord(record: Record)(implicit loggedInUserId: User) = {
    val q = records.withFilter(_.id === record.id).join(companies).on(_.companyId === _.id).filter(_._2.userid === loggedInUserId)
    val recordToUpdate = q.map { case (r, c) => r }
    val action = for {
      c <- recordToUpdate.update(record)
    } yield (c)
    ... // there are other actions in the for comprehension, removed them for clarity

我认为地图的结果是记录表中的一行(不是元组),但错误似乎表明我没有更新“单个”表。

或者有更好的方法进行查询+更新吗?

1 个答案:

答案 0 :(得分:2)

是的,您似乎尝试更新两个表。

也许你应该尝试像

这样的东西
  def updateRecord(record: Record)(implicit loggedInUserId: User): Future[Int] = {
    val recordToUpdate = records.filter(_.id === record.id)

    val q = recordToUpdate
      .join(companies).on(_.companyId === _.id)
      .filter(_._2.userid === loggedInUserId)
      .exists

    val action = for {
      c <- recordToUpdate.update(record)
//      ...
    } yield c

    for {
      isLoggedIn <- db.run(q.result)
      if isLoggedIn
      c <- db.run(action)
    } yield c

  }

您也可以尝试

  def updateRecord(record: Record)(implicit loggedInUserId: User): 
        DBIOAction[Int, NoStream, Read with Write with Transactional] = {
    val recordToUpdate = records.filter(_.id === record.id)

    val action =
      recordToUpdate
      .join(companies).on(_.companyId === _.id)
      .filter(_._2.userid === loggedInUserId)
      .exists
      .result

    (for {
      isLoggedIn <- action
      if isLoggedIn
      c <- recordToUpdate.update(record)
//    ...
    } yield c).transactionally
  }

应该在没有NoSuchElementException: Action.withFilter failed的情况下工作的变体。基于answer

  def updateRecord(record: Record)(implicit loggedInUserId: User): 
        DBIOAction[Int, NoStream, Read with Write with Transactional] = {
    val recordToUpdate = records.filter(_.id === record.id)

    val action =
      recordToUpdate
      .join(companies).on(_.companyId === _.id)
      .filter(_._2.userid === loggedInUserId)
      .exists
      .result

    action.flatMap {
      case true => for {
        c <- recordToUpdate.update(record)
      //    ...
      } yield c

      case false => DBIO.successful(0) /*DBIO.failed(new IllegalStateException)*/
    }.transactionally
  }