Skip to content

Handling timestamp fields in Typescript #561

@Maiquu

Description

@Maiquu

Issue Description

Issue is regarding this section.
Silencing typing errors regarding timestamps via specifying them in init method changes the created table schema. By default, timestamp fields created by sequelize are non nullable, but when we specify them to silence the error, timestamps are created as nullable fields.

Snippet:

Timestamp attributes managed by Sequelize (by default, createdAt, updatedAt, and deletedAt) don't need to be configured using Model.init, unfortunately Model.init has no way of knowing this. We recommend you use the minimum necessary configuration to silence this error:

User.init({
  // technically, `createdAt` & `updatedAt` are added by Sequelize and don't need to be configured in Model.init
  // but the typings of Model.init do not know this. Add the following to mute the typing error:
  createdAt: DataTypes.DATE,
  updatedAt: DataTypes.DATE,
}, { sequelize });

What was unclear/insufficient/not covered in the documentation

Current documentation misleads the user into thinking that specifying timestamp fields, does not affect runtime behavior, and is only to silence typescript.

If possible: Provide some suggestion on how we can enhance the docs

I'm not sure if this is a documentation or code error, but it if the problem is on documentation side, it should be fixed as

User.init({
  // ...
  createdAt: { type: DataTypes.DATE, allowNull: false },
  updatedAt: { type: DataTypes.DATE, allowNull: false },
}, { sequelize });

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