Issue of the type of the field is inconsistent for the "DATE" type in MySQL
See original GitHub issueWhat are you doing?
I am using Sequelize to access data from MySQL and encountering the issue of the field value format is inconsistent when setting/getting for the DATE type field in MySQL.
In simple words:
- When getting the value for a “date” field from MySQL, I get a string like “2019-05-10” and not a “Date” object,
- When setting the value, it will change the string format to a “Date” object before committing to MySQL. I reckon the format should be keeping consistent when getting/setting.
The details is as follows:
MySQL Table Structure
Table Name: “test_sequelize_date”
Fields: | id(int, pk) | date_from(date) |
---|---|---|
Test Data: | 1 | 2019-05-10 |
Test Code
const TestSequelizeDate = sequelize.define(
'test_sequelize_date',
{
id: {
type: Sequelize.INTEGER,
primaryKey:true,
autoIncrement: true,
},
date_from: {
type: Sequelize.DATE,
defaultValue: null
}
}, {
tableName: 'test_sequelize_date',
deletedAt: false,
updatedAt: false,
createdAt: false,
}
)
const row = await TestSequelizeDate.findByPk(1);
console.log(row.date_from, typeof row.date_from); // Output: "2019-05-10 string"
row.date_from = "2019-05-11";
console.log(row.date_from, typeof row.date_from); // Output: "2019-05-11T00:00:00.000Z 'object'"
What do you expect to happen?
After updating the date string “2019-05-11” to row.date_from, its value should be keeping the same format as its original value. And another thing is for the field with the “Sequelize.DATE” type, it would be great if the field type is a “Date” type in Javascript.
What is actually happening?
As we can see the source code above, when I access a “Sequelize.DATE” field, I get a string result “2019-05-10”, when I update it with a string “2019-05-11”, then, I get a “Date” type. It could make developers confused which type should use for the field and we have to manually unify the format for the date type field in MySQL.
Environment
Dialect:
- [*] mysql
- postgres
- sqlite
- [*] mssql
- any Dialect library version: mysql2@1.6.5 Database version: MySQL 8 Sequelize version: v5.8.5 Node Version: v10.15.3 OS: MacOS
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
I tested and the result of "row.set(‘date_from’, ‘2019-05-11’) is as same as “row.date_from = ‘2019-05-11’”
Hi @papb , Thank you very much for your reply. I tested it in SQL-Server and got the same result as below.