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

后端 未结 3 1284
执笔经年
执笔经年 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:35

    You already have answers as written by @pedrorijo91 and @thirstycow but i'll try to explain why this doesnt work.

    I'm havent used slick 3 but im guessing its because the map function doesnt return a consistent type for it to run update against. As a thought experiment, if you cut your call at map what do you think the type will be?

    val partialQuery:??? = people.filter(_.name === "M Odersky")
           .map(p => 
               (name, age) match {
                  case (Some(_), Some(_)) => (p.name, p.age)
                  case (Some(_), None)    => (p.name)
                  case (None   , Some(_)) => (p.age)
               }
           );
    
    val fullQuery:??? = partialQuery.update {
           (name, age) match {
              case (Some(_), Some(_)) => (name.get, age.get)
              case (Some(_), None)    => (name.get)
              case (None   , Some(_)) => (age.get)
           }    
    }
    

    The matcher returns different "shapes" at compile time which im guessing will revert to Any type.

提交回复
热议问题