How do you update multiple columns using Slick Lifted Embedding?

14,431

Solution 1

I figured it out. It should be like this

val map = Query(AbilitiesTable)
  .filter(_.id === ability_id)
  .map(ab => ab.verb ~ ab.context)

map.update(("", ""))

Typesafe, why your documentation is so bad ? I have to Google pretty much every silly thing or dig through unit-tests for hours. Please improve it. Thanks.

Solution 2

With recent versions of Slick, this way of writing it works:

Users.filter(_.id === filterId)
     .map(x => (x.name, x.age))
     .update(("john", 99))

Be careful to remember the extra parentheses if you are updating more than one property otherwise you might get a compiler warning.

Share:
14,431
expert
Author by

expert

My interests: scala kotlin java deep-learning machine-learning reactive-programming scala-cats project-reactor scalability reverse-engineering peer-to-peer. Back in 2005 I created Morpheus, the most popular file sharing application in North America. I also designed and built Dell Digital Delivery (similar to Steam for non-games) which is installed on every Dell PC world-wide and worked on improving architecture of Fivetran. Currently I'm building super-fast hybrid (batching/realtime) data integration pipeline at bitparticles.

Updated on June 17, 2022

Comments

  • expert
    expert almost 2 years

    How do you update multiple columns using Slick Lifted Embedding ? This document doesn't say much.

    I expected it to be something like this

    Query(AbilitiesTable).filter((ab: AbilitiesTable.type) => ab.id === ability_id).map((ab: AbilitiesTable.type) => (ab.verb, ab.subject)).update("edit", "doc")
    
  • BeepDog
    BeepDog almost 11 years
    Note this only works with updateable ResultSets, which require that you include the ID. If you try to use this without including the ID field in the built query, it will fail.
  • Rajeev
    Rajeev about 10 years
    In intellij comma may not work. u can alternative write like this (x.name ~ x.age)
  • windweller
    windweller almost 10 years
    Sorry...but what is AbilitiesTable? Is this that TableQuery object? The class like this: class CompanyTable(tag: Tag) extends Table[Company]?
  • null
    null about 7 years
    In Intellij version 2016, the comma seems working fine.
  • kosiara - Bartosz Kosarzycki
    kosiara - Bartosz Kosarzycki about 6 years
    Don't forget about tuples in .map(x => (...)), as well as in update(("x", "y")) so double parenthesis.