I'm using Slick with Play but am having some problems when trying to update a column value as it is not being updated, although I don't get any error back.
I have a column that tells me if the given row is selected or not. What I want to do is to get the current selected value (which is stored in a DB column) and then update that same column to have the opposite value. Currently (after many unsuccessful attempts) I have the following code which compiles and runs but nothing happens behind the scenes:
val action = listItems.filter(_.uid === uid).map(_.isSelected).result.map { selected =>
val isSelected = selected.head
println(s"selected before -> $isSelected")
val q = for {li <- listItems if li.uid === uid} yield li.isSelected
q.update(!isSelected)
}
db.run(action)
What am I doing wrong (I am new to slick so this may not make any sense at all!)
result.mapmust be aresult.flatMap. alsoval isSelected = selected.headmay throw an exception if the record does not exist. so consider usingselected.headOptioninstead as I have written in answer - shayanflatMapmakes difference when compared with themap. I also prefer your answer, but I can't seem to make it work - LuisFInt => List[String]to aList[Int]you'll get aList[List[String]]but if you flatMap the same function to the same list you'll get aList[String]]translating it to your example if you flatMap anotherDBIOAction[T]to your fistDBIOAction[G]the result is a singleDBIOAction[T]which is what you want and is the composition of the two actions. - shayan