光滑保存多个对象

时间:2018-11-25 05:09:53

标签: json scala rest slick

嘿,我有一个React应用,例如,向我的api发送命令

[
    {
        "product_id":13,
        "quantity":2
    },
    {
        "product_id":12,
        "quantity":2
    }
]

我想用它做的是,将json解析为Seq [Product] 而我的问题开始了,我不知道用光滑的方式保存该Seq。

def createMany(productList: Seq[Product]): Future[Seq[Int]] = db.run {
    for {
      p <- productList
    } yield products += p
  }

其中product是TableQuery [Product], 在考虑这样的事情,但是我得到

Type mismatch, expected: DBIOAction[NotInferedR, NoStream, Nothing], actual: Seq[JdbcProfile.this.ProfileAction[Int, NoStream, Effect.Write]]

我要退货的是我刚刚插入的产品ID的列表

1 个答案:

答案 0 :(得分:0)

tomcy是正确的,您在插入Seq时需要使用++=而不是+=。另外,如果您希望在插入后返回ID,也可以尝试:

products returning products.map(_.id) ++= productList
相关问题