如果另一个查询结果符合 Slick3.0 中的某些条件,则执行查询

execute query if another query result matches some conditions in Slick3.0

我有一个 table 类似

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 >= 10L 如果结果为真,我想在单个 db.run.

中执行一些 DBIO.seq

到目前为止我已经试过了

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

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

found : slick.lifted.Rep[Boolean] required: Boolean

我也试过这种方法

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

但是这个也会产生错误

could not find implicit value for parameter ol: slick.lifted.OptionLift[slick.driver.MySQLDriver.api.DBIOAction[Unit,slick.driver.MySQLDriver.api.NoStream,slick.driver.MySQLDriver.api.Effect.Write with slick.driver.MySQLDriver.api.Effect.Transactional],slick.lifted.Rep[Option[QO]]]

我怎样才能做到这一点?

提前致谢。

您应该尝试以下操作:

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