【问题标题】:Using function to update case class value in scala Quill使用函数更新scala Quill中的案例类值
【发布时间】:2020-04-05 13:59:17
【问题描述】:

我有以下案例类

case class Tag(key: String, value: String, modifiedDate: Date)

我有一个如下所示的数据访问对象:

class TagDao(implicit val ec: ExecutionContext, val ctx: PostgresAsyncContext[SnakeCase]) {
  def update(tag: Tag): Future[Int] = 
    performIO(
      runIO(
        quote {
          query[Tag]
            .filter(_.id == tag.id)
            .update(lift(tag))
            .returning(_.id)
        }
      )
    )
}

我希望将更新方法中TagmodifiedDate 字段替换为CURRENT_TIMESTAMP。怎么办?

另一种选择是在更新之前我在代码中手动设置modifiedDate

【问题讨论】:

  • 我最终使用了数据库触发器

标签: scala quill.io


【解决方案1】:

尝试使用案例类对象中的copy 方法并传递包含当前日期的新日期:

def update(tag: Tag): Future[Int] = 
    performIO(
      runIO(
        quote {
          query[Tag]
            .filter(_.id == tag.id)
            .update(
              lift(tag.copy(modifiedDate = new Date()))
            )
            .returning(_.id)
        }
      )
    )

【讨论】:

    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多