Skip to content

Sequelize ignores milliseconds part of DATETIME in MySQL #4205

Closed

Description

@DominikPalo

I'm using MySQL database with some columns of type DATETIME(3) (datetime with precision 3). Also in the entity model I have defined type as DATE(3). But after I set property of instance to time with millisecond part (e.g. entity.openedAt: "2015-08-29T08:39:08.12345Z"), sequelize generates SQL query, that truncates the millisecond part (...openedAt='2015-08-29 08:39:08'...).

MySQL 5.6.4 and up expands fractional seconds support for TIME, DATETIME, and TIMESTAMP values, with up to microseconds (6 digits) precision.

Activity

  1. added
    type: feature
    on Jul 29, 2015
  2. janmeier commented on Jul 29, 2015

    @janmeier
    Member

    Dates don't support length at all right now - Actually I didn't even know that dates could have a length in mysql - I'm still on 5.5 :)

    Anyhow, this should be pretty easy to do once #4186 has landed. Just change the stringify function for mysql date to check this.length to see if it should add milliseconds

  3. added a commit that references this issue on Nov 13, 2015
    87eb6a7
  4. mfbx9da4 commented on Feb 27, 2016

    @mfbx9da4

    +1 also having this problem using sequelize 3.19.2 when should this be working?

    models.scheduled_matches.update(
        {saved_timeout_time: "2016-02-27T16:49:45.458Z"},
        {where:{running_match_id: 139}})

    produces

    UPDATE `scheduled_matches` 
    SET `saved_timeout_time`='2016-02-27 16:49:45',`updatedAt`='2016-02-27 17:16:06' 
    WHERE `running_match_id` = 139
  5. sobotklp commented on Feb 27, 2016

    @sobotklp
    Contributor

    How did you declare the column saved_timeout_time in your model?

  6. sobotklp commented on Feb 27, 2016

    @sobotklp
    Contributor

    Also, which version of MySQL are you running?

  7. mfbx9da4 commented on Feb 28, 2016

    @mfbx9da4

    Model declaration:

    var scheduled_matches = sequelize.define("scheduled_matches", {
        saved_timeout_time: {type: DataTypes.DATE, allowNull: true}
    });

    Local
    mysql Ver 14.14 Distrib 5.6.27, for osx10.11 (x86_64) using EditLine wrapper
    On server
    mysql Ver 14.14 Distrib 5.5.48, for Linux (x86_64) using readline 5.1

  8. sobotklp commented on Feb 28, 2016

    @sobotklp
    Contributor

    In order to use millisecond-aware DATETIME columns, you'll have to use
    type: DataTypes.DATE(3)

  9. mfbx9da4 commented on Feb 28, 2016

    @mfbx9da4

    Oh right sorry for wasting your time! Where is this documented?

  10. sobotklp commented on Mar 2, 2016

    @sobotklp
    Contributor

    I don't believe it is documented anywhere, but I will change that tomorrow.

  11. sobotklp commented on Mar 12, 2016

    @sobotklp
    Contributor

    #5575 adds documentation to the Data Types definitions list.

  12. chaoyangnz commented on Apr 22, 2022

    @chaoyangnz

    In order to use millisecond-aware DATETIME columns, you'll have to use
    type: DataTypes.DATE(3)

    Does this affect the MySQL column type or just how Sequelize is aware of milliseconds?

  13. ephys commented on Apr 22, 2022

    @ephys
    Member

    Normally both

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions