I have a simple MySQL db with a single row in the REG_PERIODS table. When I run this query I get the row back.
val getRegPeriodAction = regPeriods.sortBy(_.endDate).result.head.map { regPeriod ⇒
Logger.debug(s"regPeriod = ${regPeriod}")
regPeriod
}
However, when I run this query, I get nothing, in fact the debug message isn't even being reached!
val getRegPeriodAction = regPeriods.sortBy(_.endDate).result.headOption.map { regPeriodOption ⇒
Logger.debug(s"regPeriodOption = ${regPeriodOption}")
regPeriodOption match {
case Some(regPeriod) ⇒ regPeriod
}
}
I'm using play-slick 1.0.0. Am I misunderstanding something here? Should I receive a Some(regPeriod) when using headOption?
EDIT
I've tried running the the headOption
query in db.run( ... )
and sending the result to another function, like so:
def handleResult(opt: Option[RegistrationPeriod]) {
Logger.debug(s"received $opt")
}
for {
r <- db.run( regPeriods.sortBy(_.endDate.desc).result.headOption )
_ <- handleResult( r )
} ...
The odd thing is that handleResult is never called! As soon as I change the code to call head
instead of headOption
everything works as expected.