使用 DBAction 插入 DB 时不产生任何...产生

Yield nothing in for ... yield when inserting into DB using DBAction

当使用 for ... yield 构造(见下文)在数据库中插入对象时,我希望能够 'yield nothing' 满足某些条件。我还在学习 Scala,对 Slick 的经验还很少。

val usersTable = TableQuery[dbuserTable]
val inserts = for (user <- x.userList) yield {
  if (user.someCondition == true) {
      val userRow = userClass(user.name, user.id)
      usersTable += userRow
  }
  else {
    //yield nothing
  }      
}
DBAccess.db.run(DBIO.seq(inserts: _*))

恐怕我遗漏了一些关于 yield 循环如何导致 DBAction 对象序列的信息。

尝试

val inserts = for (user <- x.userList) yield {
  if (user.someCondition == true) {
    val userRow = userClass(user.name, user.id)
    usersTable += userRow
  }
  else {
    DBIO.successful(0)
  }
}

Int in DBIOAction[Int, NoStream, Effect.Write] 表示插入的行数。