mForest
mForest

Reputation: 88

exists query in slick

i have query like bellow

  override def isOnList(id: Long, value: String)(implicit ex: ExecutionContext): DBIO[Boolean] = {
    tableQuery.filter(e => e.id === id && e.isActive && e.value === value).exists.result
  }

But it returns false when the table does not any rows with id === id. I would like to change this. My modification is

  override def isOnList(id: Long, value: String)(implicit ex: ExecutionContext): DBIO[Boolean] = {
    tableQuery.filter(e => e.id === id && e.isActive).result.flatMap {
      case seq if seq.isEmpty => DBIOAction.successful(true)
      case seq => DBIOAction.successful(seq.exists(_.value==value))
    }
  }

I am new with slick, so i would like to know that is it good solution or not (i coud not find any information.)? Oh i forgot, Id is not unique.

Upvotes: 0

Views: 2299

Answers (1)

joesan
joesan

Reputation: 15385

What is wrong with your first option? It seems to be perfectly valid!

def isOnList(id: Long, value: String)(implicit ex: ExecutionContext) : Future[Boolean] = 
    db.run(User.filter(e => e.id === id && e.isActive && e.value === value).exists.result)

This method should automatically return true or false depending on the filter condition!

Upvotes: 3

Related Questions