sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

Model.build strips off timestamps

Open thebigredgeek opened this issue 8 years ago • 7 comments

Thanks for wanting to report an issue you've found in Sequelize. Please delete this text and fill in the template below. If unsure about something, just do as best as you're able.

Note that it will be much easier for us to fix the issue if a test case that reproduces the problem is provided. Ideally this test case should not have any external dependencies. We understand that it is not always possible to reduce your code to a small test case, but we would appreciate to have as much data as possible. Thank you!

Github should only be used for feature requests and bugs - all questions belong on StackOverflow, Slack, or Google groups.

This template is for submitting issues - if you want to submit a feature request you may skip this template

What you are doing?

Post a minimal code sample that reproduces the issue, including models and associations

console.log(instance.dataValues, instance.toJSON(), Model.build(instance.toJSON()));

What do you expect to happen?

The fields should be identical in all 3 cases. The instance is a freshly selected row out of the database. No updates have occurred.

What is actually happening?

After passing instance.toJSON() into Model.build, the new returned instance does not contain createdAt or updatedAt timestamps. I am manually adding these stamps, and am not using the timestamps option.

Dialect: postgres Database version: 9 Sequelize version: 3.30

thebigredgeek avatar Feb 24 '17 09:02 thebigredgeek

For createdAt and updatedAt are the fields updating when you save the record versus just building it?

durango avatar Mar 17 '17 23:03 durango

Building a model from cached data will strip off timestamps.

Member.build(data, { isNewRecord: false })

seancheung avatar Aug 25 '17 04:08 seancheung

Solution here:

Model.$build = function(values, options) {
    const instance = this.build(values, options);
    if (
        this.options.timestamps === true &&
        options && 
        options.isNewRecord === false
    ) {
        instance.dataValues.createdAt = values.createdAt;
        instance.dataValues.updatedAt = values.updatedAt;
        instance._changed = {};
        instance._previousDataValues = Object.assign(
            {},
            instance.dataValues
        );
        instance._options.raw = true;
        instance._options.attributes = Object.keys(values);
    }

    return instance;
};

seancheung avatar Aug 25 '17 05:08 seancheung

This is still an issue, and an annoying one.

andrewzuk avatar Jul 18 '19 15:07 andrewzuk

@thebigredgeek @andrewzuk What is the use case for having createdAt and updatedAt fields that are manually created, instead of automatically created with the timestamps option?

papb avatar Jul 18 '19 19:07 papb

@papb For example: I use raw query to make stream, and then build model instances in stream transformation manually. I just want stream + getters / setters / toJSON of my ORM.

HKFiliatix avatar Aug 22 '19 10:08 HKFiliatix

Use case: Creating Model instances that are injected into the code by my unit tests. I should be able to define all known Model fields in the build function's value parameter and read them from the resulting Model instance when build returns..

kf6kjg avatar Jun 13 '25 17:06 kf6kjg