3

I have the following model:

case class Bar(id: Int, code: Option[String])

class BarTable(tag: Tag) extends Table[Bar](tag, "bar") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)

    def code = column[Option[String]]("code")

    def * = (id, code).mapTo[Bar]
}

object BarTable extends TableQuery(new BarTable(_)) {
    def create(bar: Bar): DBIO[Bar] = {
        this returning this.map(_.id) into ((t, id) => t.copy(id = id)) += bar
    }
}

I try to insert multiple Bar in one transaction:

def foo(codes: Seq[String]): Seq[Bar] = exec {
    for {
        bars <- DBIO.sequence(codes.map{code=>
            BarTable.create(Bar(0, Some(code)))
        })
    } yield bars
}

private final def exec[T](program: DBIO[T]): T = Await.result(connection.run(program.transactionally), Duration.Inf)

But the code above yields error in my IDE:

No implicits found for parameter cbf: compat.Factory[Bar, Seq[Bar]]

What do I miss?

Do note that if I only insert one (that is, no map/DBIO.sequence), the code compiles.

wiradikusuma
  • 1,930
  • 4
  • 28
  • 44

0 Answers0