Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `firstOption` with slick 3

Tags:

scala

slick

I am trying to run a query like .filter(_.id === 1).firstOption but the compiler complains there is no symbol firstOption. Was this removed in slick 3? What can I use instead?

like image 782
simao Avatar asked Jun 04 '15 10:06

simao


1 Answers

To limit the number of results before calling result, use take(num). For example like this:

val result: Future[Option[Whatever]] = db.run((query.filter(_.id === 1).take(1)).result).map(_.headOption)

According to the official docs, the above statement boils down using headOption on the result method.

val result: Future[Option[Whatever]] = db.run((query.filter(_.id === 1)).result.headOption)

query.result returns an object of type DBIOAction. An action in slick is something that can be executed on a database. The actual execution is done by passing the action to db.run() or db.stream(). You can find a more detailed explanation here: http://slick.typesafe.com/doc/3.0.0/api/index.html#slick.dbio.DBIOAction

like image 126
Roman Avatar answered Sep 29 '22 10:09

Roman