I tried to reproduce the issue, but failed to get any errors when the server connects to local mysql and Azure ClearDB using node-mysql
. I suggest you to check the mysql carefully by using some third-party GUI admin tools like HeidiSQL for Windows or Dbeaver for any platform support Java.
You can download them at http://www.heidisql.com/ and http://dbeaver.jkiss.org/
I think you need to add symbol ;
for each JS sentences and use var
instead of const
for declaring a varibale because of following the JavaScript Specification and the node-mysql doc, although it works fine without symbol ;
on NodeJS.
Please refer to these docs below:
When I connected MySQL and loaded table schema to create a table, I found that your SQL DDL sentence is not correct. Please refer to the mysql offical docs http://dev.mysql.com/doc/refman/5.6/en/create-table.html and http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html.
Note: For date type DATETIME
& TIMESTAMPE
, the mysql offical docs says:
DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP cannot be used with DATETIME columns.
So Here are the table ddl sql and code updated.
Table DDL:
CREATE TABLE users (
id BIGINT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(40),
last_name VARCHAR(40),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id)
)
Code:
var mysql = require('mysql');
var connection_parames = "mysql://user:password@host/test_database";
var connection = mysql.createConnection(connection_parames);
var table_ddl = "CREATE TABLE users ("+
"id BIGINT NOT NULL AUTO_INCREMENT,"+
"first_name VARCHAR(40),"+
"last_name VARCHAR(40),"+
"created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,"+
"PRIMARY KEY(id)"+
")";
connection.query(table_ddl, function(err, result) {
if (err) {
console.log(err);
}
console.log(result);
});