Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Title basically says it all.

I am predominantly interested in the update case. Suppose we are trying update a record that has a timestamp field, and we wish to set that field to the timestamp of when the record is updated. Is there a way to do this?

like image 876
gdoug Avatar asked Feb 17 '17 21:02

gdoug


1 Answers

After some experimentation, I've figured out the proper solution. You can use multiple .update(...) calls on the same query without screwing anything up, so long as you don't use multiple objects (this includes knex.raw). You can combine one object style call with field/value style calls, such as:

knex('table').update({ x: 1, y: 2 }).update('modified_at', knex.fn.now()).where(...) // and so on.
like image 130
gdoug Avatar answered Dec 28 '22 09:12

gdoug