KnexJS:如何 insert/update 具有当前时间戳的时间戳字段?

KnexJS: How do you insert/update a timestamp field with current timestamp?

标题基本上说明了一切。

我主要对更新案例感兴趣。假设我们正在尝试更新具有时间戳字段的记录,并且我们希望将该字段设置为更新记录时的时间戳。有办法吗?

经过一些实验,我找到了正确的解决方案。您可以对同一个查询使用多个 .update(...) 调用而不会搞砸任何事情,只要您不使用多个对象(包括 knex.raw)。您可以将一个对象样式调用与 field/value 样式调用结合起来,例如:

knex('table').update({ x: 1, y: 2 }).update('modified_at', knex.fn.now()).where(...) // and so on.