Slick 3.0.0 - update row with only non-null values

后端 未结 3 1111
攒了一身酷
攒了一身酷 2021-01-13 15:37

Having a table with the columns

class Data(tag: Tag) extends Table[DataRow](tag, \"data\") {
  def id = column[Int](\"id\", O.PrimaryKey)
  def name = column         


        
3条回答
  •  孤城傲影
    2021-01-13 15:45

    Building on JonasAnso's answer, converting that to slick v3.0+, and putting it into a transaction:

      def partialUpdate(id: Int, name: Option[String], login: Option[String]): Future[Int] = {
        val selectQ = users.filter(_.id === id)
    
        val query = selectQ.result.head.flatMap { data =>
          selectQ.update(data.patch(name, login))
        }
    
        db.run(query)
      }
    

提交回复
热议问题