scalaslick

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


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


Solution

  • 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