Returning Affected Rows (Int) in Slick 3.0

In previous versions of Slick, there was an insert function defined in TableQuery that returned the result of the insert. I go over to the new API and do something like:

DBIO.seq(someTable += someValue)

but it has a type

dbio.DBIOAction[Unit, NoStream, Write]

How can I return the affected rows?

change

The problem here is that the types do not line up as I expected. Here Scala sees types as:

val q: PostgresDriver.DriverAction[PostgresDriver.InsertActionExtensionMethods[(String, String)]#SingleInsertResult, NoStream, Write] = Admins.tableQuery += ((username, grantor))
val seq: dbio.DBIOAction[Unit, NoStream, Write] = DBIO.seq(q)
db.run(seq)

That doesn't make sense to me. If it qhas a type DriverAction[PostgresDriver.InsertActionExtensionMethods[(String, String)]#SingleInsertResult...where SingleInsertResult is a type alias Int, then mine DBIO.seqshould not return a DBIOAction[Int, NoStream, Write]?

+4
source share
1 answer

Check out these examples.

This is not very different from slick 2.x, but now everything returns Futures.

, - ( , )

val users = TableQuer[Users]
// this is the query
val q = (users += User(None, "Stefan", "Zeiger"))
// now you run it
db.run(q).map{ affectedRows =>
  case 1 => println("success")
  case _ => println("oops")
}
+1

All Articles