Upsert in Slick

前端 未结 2 1438
野的像风
野的像风 2020-12-24 12:42

Is there a way I can neatly do an upsert operation in Slick? The following works but is too obscure/verbose and I need to explicitly state the fields that should be updated:

2条回答
  •  星月不相逢
    2020-12-24 13:02

    Apparently this is not (yet?) in Slick.

    You might however try firstOption for something a bit more idiomatic:

    val id = 1
    val now = new Timestamp(System.currentTimeMillis)
    val user = Users.filter(_.id is id)
    user.firstOption match {
      case Some((_, created, _)) => user.update((id, created, now))
      case None => Users.insert((id, now, now))
    }
    

提交回复
热议问题