Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating MySQL table with CURRENT_TIMESTAMP in node.js

Tags:

node.js

mysql

I am using node-mysql to update a MySQL database table from node.js. I want to update the timestamp column of a row with CURRENT_TIMESTAMP. However, no changes seem to be made by node.js using the following code:

Node.js Code

client.query('UPDATE listings SET job_checkout_timestamp = CURRENT_TIMESTAMP WHERE listing_id = 1515');

But works if I were to replace CURRENT_TIMESTAMP with a javascript time function, like new Date()

client.query('UPDATE listings SET job_checkout_timestamp = ? WHERE listing_id = 1515', [ new Date() ]);

However, if I were to execute the same SQL query directly into mysql (using Navicat), the row gets updated with the current timestamp!

Direct SQL Query

UPDATE listings SET job_checkout_timestamp = CURRENT_TIMESTAMP WHERE listing_id = 1515;

Did something go wrong somewhere?

like image 347
Nyxynyx Avatar asked Nov 05 '22 10:11

Nyxynyx


1 Answers

It seems that node.js might convert the input variables into string,
and make the current_timestamp as string rather than mysql variables,
by replace the current_timestamp to synonym function call like :-

now()
current_timestamp()

should fix the problem

like image 167
ajreal Avatar answered Nov 10 '22 07:11

ajreal