【问题标题】:execute query if another query result matches some conditions in Slick3.0如果另一个查询结果匹配 Slick3.0 中的某些条件,则执行查询
【发布时间】:2016-10-08 13:02:10
【问题描述】:

我有一张类似的桌子

final class FooTable(tag: Tag) extends Table[Foo](tag, "foo") {

  def id = column[Int]("id", O.PrimaryKey)

  def amount = column[Long]("amount")

  def createTs = column[Timestamp]("create_ts")

  def updateTs = column[Timestamp]("update_ts")

  def * = (id, amount, status, createTs, updateTs) <> (Foo.tupled, Foo.unapply)

}

并尝试检查TableQuery[FooTable].map{_.amount}.sum &gt;= 10L,如果结果为真,我想在单个db.run 中执行一些DBIO.seq

到目前为止我已经尝试过

  val action = for {
    bar <- (foos.map{_.amount}.sum <= givenLongValue)
    result <- DBIO.seq("//some queries here") if bar
  } yield result

但是由于这个错误,这不起作用

找到:slick.lifted.Rep[Boolean] 需要:布尔

我也试过这种方式

(fooes.map{_.amount}.sum <= amount).filter{_}.map{ x =>
      DBIO.seq("some actions")
}

但是这个也会产生错误

找不到参数 ol 的隐含值: slick.lifted.OptionLift[slick.driver.MySQLDriver.api.DBIOAction[单位,slick.driver.MySQLDriver.api.NoStream,slick.driver.MySQLDriver.api.Effect.Write 和 slick.driver.MySQLDriver.api.Effect.Transactional],slick.lifted.Rep[Option[QO]]]

我怎样才能做到这一点?

提前致谢。

【问题讨论】:

    标签: scala slick playframework-2.5 play-slick


    【解决方案1】:

    您应该尝试以下方法:

    val query = TableQuery[FooTable]
    val givenLongValue = 10L
    val action = query.map(_.amount).sum.result.flatMap {
      case Some(sum) if sum <= givenLongValue => DBIO.seq(...)
      case _ => DBIO.successful(...) // You should decide here what you want to return in case of `None` or `sum > 10L`
    }.transactionally
    

    【讨论】:

      猜你喜欢
      • 2010-10-31
      • 2018-10-17
      • 1970-01-01
      • 2023-02-07
      • 2017-11-14
      • 1970-01-01
      • 2021-07-03
      • 2018-09-12
      • 2018-04-29
      相关资源
      最近更新 更多