Search code examples
scalaslickslick-3.0

How to create generic method to update multiple columns in Slick?


I have this generic method to update one column

def updateColumn[V](id: Int, 
    column: Table[UserRow] => Rep[V], 
    value:  V)(implicit shape: Shape[_ <: FlatShapeLevel, Rep[V], V, _]) =
    userTableQuery.filter(user => user.id  === id).map(column).update(value))

So, I can use it like this

updateColumn(1, user => user.firstName, "FirstName")

I would like to use it for multiple columns

updateColumn(1, user => (user.firstName, user.lastName), ("FirstName", "LastName"))

But It has a compile error

No matching Shape found
Required level: slick.jdbc.PostgresProfile.api.FlatShapeLevel
Source type: slick.lifted.Rep[(String, String)]

Is it possible to create such method?


Solution

  • Turns out, it is possible

    def update[F, G, K](id: Int, 
      columns: Table[UserRow] => F, 
      value: K)(implicit shape: Shape[_ <: FlatShapeLevel, F, K, G]) =
        userTableQuery.filter(user => user.id  === id).map(columns).update(value)