Skip to content

Default value for created_at or updated_at isn't added when using underscored: true #10754

Closed

Description

@mahsan3-zz

What are you doing?

Trying to create some models with default values for the created_at and updated_at, using the global configuration option of underscored: true.

  created_at: {
      type: DataTypes.DATE,
      defaultValue: sequelize.literal('NOW()')
    },
    updated_at: {
      type: DataTypes.DATE,
      defaultValue: sequelize.literal('NOW()')
    }

What do you expect to happen?

The default value for the columns created_at and updated_at should be CURRENT_TIMESTAMP

What is actually happening?

There is no default value added for these columns; however, removing the option of underscored: true fixes this.

Dialect: mysql
__Dialect version: 8.0.15
__Database version: 8.0.15
__Sequelize version: 5.3.0
__Tested with latest release: Yes

Activity

  1. sbonami commented on Apr 12, 2019

    @sbonami
    Contributor

    There's a timestamps configuration and specific configuration available for those fields.

    As for relying on the DBMS to actually manage this, Sequelize isn't yet setup for such a function: https://github.com/sequelize/sequelize/blob/v5.3.0/lib/utils.js#L274-L293

  2. mahsan3-zz commented on May 3, 2019

    @mahsan3-zz
    Author

    Not to sure how relevant the configuration links are, one is for migrations and the other is simple configurations (i.e change what it's called instead of createdAt or if those columns should be added).

    When using the model definitions below, without the underscore: true option, the actual table generated has the value 'CURRENT_TIMESTAMP' for default. With the underscore: true, this stops happening.

     createdAt: {
          type: DataTypes.DATE,
          defaultValue: sequelize.literal('NOW()')
        },
        updatedAt: {
          type: DataTypes.DATE,
          defaultValue: sequelize.literal('NOW()')
        }
    

    But not really an issue for me at this point, have just proceeded without that option.

  3. papb commented on Aug 29, 2019

    @papb
    Member

    Closing in favor of #11225

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions