Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(upsert): do not overwrite an explcit created_at during upsert #13593

Merged
merged 2 commits into from Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/model.js
Expand Up @@ -2480,7 +2480,7 @@ class Model {
const now = Utils.now(this.sequelize.options.dialect);

// Attach createdAt
if (createdAtAttr && !updateValues[createdAtAttr]) {
if (createdAtAttr && !insertValues[createdAtAttr]) {
const field = this.rawAttributes[createdAtAttr].field || createdAtAttr;
insertValues[field] = this._getDefaultTimestamp(createdAtAttr) || now;
}
Expand Down
9 changes: 9 additions & 0 deletions test/integration/model/upsert.test.js
Expand Up @@ -302,6 +302,15 @@ describe(Support.getTestDialectTeaser('Model'), () => {
clock.restore();
});

it('does not overwrite createdAt when supplied as an explicit insert value when using fields', async function() {
const clock = sinon.useFakeTimers();
const originalCreatedAt = new Date('2010-01-01T12:00:00.000Z');
await this.User.upsert({ id: 42, username: 'john', createdAt: originalCreatedAt }, { fields: ['id', 'username'] });
const user = await this.User.findByPk(42);
expect(user.createdAt).to.deep.equal(originalCreatedAt);
clock.restore();
});

it('does not update using default values', async function() {
await this.User.create({ id: 42, username: 'john', baz: 'new baz value' });
const user0 = await this.User.findByPk(42);
Expand Down