如何在DBIOaction SLICK中展平序列序列?

时间:2018-02-02 20:45:47

标签: scala slick flatten

嘿伙计们我是浮油新手,我怎么能压扁这个序列序列?这样就可以返回注释代码

    def insertIfNotExists(mapCountryStates: Map[String, Iterable[StateUtil]]): Future[Seq[Seq[StateTable]]] /*: Future[Seq[StateTable]]*/ = {
    val interaction = DBIO.sequence(mapCountryStates.toSeq.map { case (alpha2Country, statesUtil) =>
      val codes = statesUtil.map(_.alpha3Code)
      for {
        countryId <- Countries.filter(_.alpha2Code === alpha2Country).map(_.id).result.head
        existing <- States.filter(s => (s.alpha3Code inSet codes) && s.countryId === countryId).result
        stateTables = statesUtil.map(x => StateTable(0L, x.name, x.alpha3Code, countryId))
        statesInserted <- StatesInsertQuery ++= stateTables.filter(s => !existing.exists(x => x.alpha3Code == s.alpha3Code && x.countryId == s.countryId))
      } yield existing ++ statesInserted
    })

    db.run(interaction.transactionally)
  }

如果我在这里写的话:

val interaction = DBIO.sequence(...)。展平

或在这里:

db.run(相互作用。的扁平化 .transactionally)

[error]无法证明Seq [Seq [StateRepository.this.StateTableMapping#TableElementType]]&lt;:&lt; slick.dbio.DBIOAction [R2,S2,E2]。

但是当应用程序运行时,因为IDE没有将其检测为错误: enter image description here

enter image description here

我使用 DBIO.fold:

更新我的定义

enter image description here

2 个答案:

答案 0 :(得分:2)

看起来你可能会追随DBIO.fold。这提供了一种采取许多操作并将其降低到单个值的方法。在这种情况下,您的单个值是来自Seq[StateTable]的{​​{1}}。

这可能是一个概述......

Seq[Seq[StateTable]]

看起来类型将使用折叠排列。希望它在你的情况下有用。

Chapter 4 Essential Slick中有关于折叠的更多信息。

答案 1 :(得分:1)

一个可行的解决方案应该是在Future完成后展平序列:

def insertIfNotExists(mapCountryStates: Map[String, Iterable[StateUtil]]): Future[Seq[StateTable]] = {
  val interaction = DBIO.sequence(mapCountryStates.toSeq.map { case (alpha2Country, statesUtil) =>
    val codes = statesUtil.map(_.alpha3Code)
    for {
      countryId <- Countries.filter(_.alpha2Code === alpha2Country).map(_.id).result.head
      existing <- States.filter(s => (s.alpha3Code inSet codes) && s.countryId === countryId).result
      stateTables = statesUtil.map(x => StateTable(0L, x.name, x.alpha3Code, countryId))
      statesInserted <- StatesInsertQuery ++= stateTables.filter(s => !existing.exists(x => x.alpha3Code == s.alpha3Code && x.countryId == s.countryId))
    } yield existing ++ statesInserted
  })

  db.run(interaction.transactionally).map(_.flatten)
}