-
-
Notifications
You must be signed in to change notification settings - Fork 627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime fields returned as date when time is 00:00:00 #1040
Comments
could you post simple self contained example, including schema creation and initial data insert? |
Sure here is a standalone snippet //running with node 10.16.0 using mysql2 ^1.7.0
const mysql = require('mysql2/promise');
async function main() {
let db = await mysql.createConnection({
host: 'localhost',
port: '3306',
user: 'root',
password: 'root'
});
await db.query("SET time_zone='+00:00'");
await db.query('DROP DATABASE IF EXISTS test_db');
await db.query('CREATE DATABASE IF NOT EXISTS test_db');
await db.close();
db = await mysql.createConnection({
host: 'localhost',
port: '3306',
user: 'root',
password: 'root',
database: 'test_db',
dateStrings: true,
timezone: '+00:00'
});
await db.query('CREATE TABLE IF NOT EXISTS mytbl (id INT AUTO_INCREMENT, created DATETIME NOT NULL, PRIMARY KEY (id)) ENGINE=INNODB');
await db.execute('INSERT INTO mytbl (created) VALUES (?)',['2019-10-01 00:00:00']);
let [result] = await db.execute('SELECT * FROM mytbl', []);
console.log('execute result',result[0]);
[result] = await db.query('SELECT * FROM mytbl', []);
console.log('query result',result[0]);
}
main(); |
Data in db has DATETIME type, but actual value only contain 4 bytes ( 2 for year, 1 for month and 1 for day ) - this is where it gets deserialized to a string ( note that there is no intermediate conversion to JS Date when you pass node-mysql2/lib/packets/packet.js Lines 303 to 306 in aa0f727
I agree, result of I guess the fix should be to append Do you want to volunteer to fix this @lirancr ? |
Of course. Any reason why query behaves differently ? |
execute and query serialise responses very differently, for query results come as text and for execute - binary encoded In the above example when |
Done, see associated PR |
Travis CI tests fail for some reason, I'll have a look tomorrow |
any update on this ? |
hi, when will #1147 be merged? |
Setting a DATETIME field with the value 2019-10-01 00:00:00
Then retrieve the data using the execute command:
const [result] = connection.execute('SELECT * FROM my_table', [])
result will equal to '2019-10-01' instead of '2019-10-01 00:00:00' although it's a DATETIME field and not merely a DATE
note that when using query the behavior is as expected
The text was updated successfully, but these errors were encountered: