0
votes

I'm new to scala and slick. I have to write an update statement to update a value in properties table.

def updateValue(key: String, value: String) = Try(Tables.Properties.filter(_.key === key).map(j => j.value).update(Option(value)) ) match {
   case success => println("Record sucessfully updated")
   case failure => println("An error occurred")
}

The above method is executing without any error but when I check in the table, it is not updating any value in the table. Is there anything else I have to add here?

2
it seems that you already found the problem. you were missing the db.run. Hoping that you can find it useful have a look at this tutorial I wrote a while ago: pedrorijo.com/blog/play-slick with the code example at github.com/pedrorijo91/play-slick3-steps/tree/step3pedrorijo91
Thanks. I will go though itRajashree Gr

2 Answers

1
votes

I got the problem. I should use db.run() to update.

def updateValue2(key: String, value: String) = {
  val action = Tables.Properties.filter(_.key === key).map(j => j.value).update(Option(value))
  db.run(action)
}
1
votes

You want Success(x) and Failure(x), not success/failure.

What you have when you use a lowercase name after the case is a variable populated with the result, which means that case success will always execute (i.e. whatever happens the variable success will be populated) and case failure will never execute.

The variable in the parentheses is populated with the successful result (i.e. Success(a), a holds the result) or the exception thrown (i.e. Failure(b), b holds the exception).