光滑的DBIO序列无法编译

时间:2019-05-12 06:17:32

标签: scala generics playframework slick

我正在尝试将模型ProductCategory对象保存在数据库中。保存时,categoriesIdSeq

case class ProductCategory(productItemId: ProductItemId, categoryies: CategoryId, filterName: FilterName)

/*Inside another object starts*/
def saveCategoriesId(productItemId: ProductItemId, categoryId: Seq[CategoryId], filterName: FilterName):
      Future[Seq[ProductItemId]] =
        db.run({
          DBIO.sequence(categoryId.map(id => save(ProductCategory(productItemId, id, filterName))))
        })

def save(productCategory: ProductCategory): DBIO[ProductItemId] = 
      query returning query.map(_.productItemId) += productCategory

出现以下错误:

[error] /Users/vish/Work/jd/app/service/ProductCategoryService.scala:20:35: type mismatch;
[error]  found   : Seq[slick.dbio.DBIOAction[models.ProductItemId,slick.dbio.NoStream,Nothing]]
[error]  required: Seq[slick.dbio.DBIOAction[models.ProductItemId,slick.dbio.NoStream,E]]
[error]       DBIO.sequence(categoryId.map(id => save(ProductCategory(productItemId, id, filterName))))

Playframework版本是2.6。这个问题不是this的重复。这个问题阻碍了进一步的发展。在回答时,请评论是否正确保存categoriesId

1 个答案:

答案 0 :(得分:3)

通常在Scala中,编译错误found: Nothing, required: E表示编译器无法推断某些类型。尝试手动指定一些类型参数

db.run({
  DBIO.sequence[ProductItemId, Seq, All](categoryId.map(id => save(ProductCategory(productItemId, id, filterName))))
})

db.run({
  DBIO.sequence(categoryId.map[DBIO[ProductItemId], Seq[DBIO[ProductItemId]]](id => save(ProductCategory(productItemId, id, filterName))))
})

或引入局部变量(然后编译器将能够自行推断类型)

val actions = categoryId.map(id => save(ProductCategory(productItemId, id, filterName)))
db.run({
  DBIO.sequence(actions)
})