Home > Mobile >  How to change many fields at the same time in Slick update query?
How to change many fields at the same time in Slick update query?

Time:09-05

Lets say I have a table for movies which has many fields such as id, the name of the movie,year it was created etc. I know that the following works:

val updateMovieNameQuery = SlickTables.movieTable.filter(_.id === movieId).map(_.name).update("newName")

Is there any way how to update two or more fields in this way ? I tried the following but this doesnt work.

val updateMovieNameQuery = SlickTables.movieTable.filter(_.id === movieId).map(_.name,_.year).update("newName",1997)

I

CodePudding user response:

Your answer is pretty close, but you need to extract the fields as a tuple and then pass a new tuple to update:

val updateMovieNameQuery = SlickTables
  .movieTable
  .filter(_.id === movieId)
  .map(m => (m.name, m.year)) // Create tuple of field values
  .update(("newName",1997)) // Pass new tuple of values
  • Related