question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue of the type of the field is inconsistent for the "DATE" type in MySQL

See original GitHub issue

What 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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ukalpacommented, Jul 25, 2019

What happens if you use row.set('date_from', "2019-05-11") instead?

(I am guessing probably the same behaviour, but being sure would probably be helpful for someone who wants to tackle this issue)

I tested and the result of "row.set(‘date_from’, ‘2019-05-11’) is as same as “row.date_from = ‘2019-05-11’”

1reaction
ukalpacommented, Jul 25, 2019

Hello, thanks for the report, can you please check if this happens with another dialect as well?

Hi @papb , Thank you very much for your reply. I tested it in SQL-Server and got the same result as below.

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the DATETIME Format Not Consistent in My Destination?
The inconsistency in DATETIME formats is due to a change on October 2019. Any connectors created before October 2019 sync DATETIME data to...
Read more >
MySQL inconsistent date ordering - hibernate - Stack Overflow
It's as if MySQL is unable to distinguish the difference between the dates with more than second accuracy when the query is in...
Read more >
Unexpected behaviour for date comparison with constant strings
Description: When comparing DATEs with constant strings, MySQL first tries to convert the string to date and do the comparison as DATETIME.
Read more >
MySQL Bugs: #79259: Get inconsistent result for invalid date ...
This is not a bug. Basically when you do: SET sql_mode = 'ALLOW_INVALID_DATES'; you cannot expect anything good after it. CAST('2015-13-10' AS ...
Read more >
MySQL 5.7 Reference Manual :: 11.2 Date and Time Data Types
By default, when MySQL encounters a value for a date or time type that is out of range or otherwise invalid for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found