Reputation: 15
I was using slick 3.2.1. I use operatorId as Foreign Key.
My code is something like that
def changeCommentStatus(changeRequest: StatusUpdateRequest(implicit ec: ExecutionContext): Future[Int] = {
val changeStatusQuery = comments
.filter(_.id inSetBind changeRequest.commentIds)
.filter(_.status === changeRequest.prevStatus)
.map(c => (c.operatorId, c.updatedAt, c.status))
.update((Some(changeRequest.operatorId), Some(new Timestamp(System.currentTimeMillis())), changeRequest.status))
db.run(changeStatusQuery)
}
Try(changeCommentStatus(changeRequest)) match {
case Success(response) => bla bla bla
case Failure(f) => bla bla bla
I use operatorId which violate Foreign Key but Try cannot be failure. How can i fix that?
Upvotes: 0
Views: 319
Reputation: 19527
Use DBIOAction#asTry
to convert the DBIOAction[Int]
to a DBIOAction[Try[Int]]
, then flatMap
the result:
def changeCommentStatus(...): Future[Int] = {
val changeStatusQuery = comments
.filter(...)
.filter(...)
.map(...)
.update(...)
.asTry
.flatMap {
case Success(res) => DBIO.successful(res)
case Failure(t) => DBIO.failed(t)
}
db.run(changeStatusQuery)
}
Then you can pattern match on the Future
in the following manner:
changeCommentStatus(changeRequest).onComplete {
case Success(response) => ...
case Failure(t) => ...
}
Upvotes: 1