How do you run a patch/partial database UPDATE in Scala Slick?

后端 未结 3 1286
执笔经年
执笔经年 2021-02-02 13:53

We\'d like to run a patch/partial UPDATE with Slick (3.0.0) so that we only modify some of the fields in a record. Exactly which fields will be updated exactly will

3条回答
  •  面向向阳花
    2021-02-02 14:54

    Why not do the pattern matching before constructing the update query?

    def patchPerson(name: Option[String], age: Option[Int]) = {
       val query = people.filter(_.name === "M Odersky")
       (name, age) match {
         case (Some(name), Some(age)) =>
           query.map(p => (p.name, p.age)).update(name, age)
         case (Some(name), None) =>
           query.map(p => p.name).update(name)
         case (None, Some(age)) =>
           query.map(p => p.age).update(age)
       }
    }
    

提交回复
热议问题