不要怂,就是干,撸起袖子干!

Commit 33c5a31f by Chad Huntley Committed by Sushant

fix(upsert): use correct timestamp field names when generating query (#8088)

1 parent 9bfbee6c
Showing with 39 additions and 2 deletions
......@@ -2192,10 +2192,12 @@ class Model {
// Attach createdAt
if (createdAtAttr && !updateValues[createdAtAttr]) {
insertValues[createdAtAttr] = this._getDefaultTimestamp(createdAtAttr) || now;
const field = this.rawAttributes[createdAtAttr].field || createdAtAttr;
insertValues[field] = this._getDefaultTimestamp(createdAtAttr) || now;
}
if (updatedAtAttr && !insertValues[updatedAtAttr]) {
insertValues[updatedAtAttr] = updateValues[updatedAtAttr] = this._getDefaultTimestamp(updatedAtAttr) || now;
const field = this.rawAttributes[updatedAtAttr].field || updatedAtAttr;
insertValues[field] = updateValues[field] = this._getDefaultTimestamp(updatedAtAttr) || now;
}
// Build adds a null value for the primary key, if none was given by the user.
......
......@@ -28,9 +28,19 @@ describe(Support.getTestDialectTeaser('Model'), () => {
secretValue: {
type: DataTypes.INTEGER,
allowNull: false
},
createdAt: {
type: DataTypes.DATE,
field: 'created_at'
}
});
const UserNoTime = current.define('UserNoTime', {
name: DataTypes.STRING
}, {
timestamps: false
});
before(function() {
this.query = current.query;
current.query = sinon.stub().returns(Promise.resolve());
......@@ -56,6 +66,31 @@ describe(Support.getTestDialectTeaser('Model'), () => {
})).not.to.be.rejectedWith(current.ValidationError);
});
it('creates new record with correct field names', () => {
return User
.upsert({
name: 'Young Cat',
virtualValue: 999
})
.then(() => {
expect(Object.keys(self.stub.getCall(0).args[1])).to.deep.equal([
'name', 'value', 'created_at', 'updatedAt'
]);
});
});
it('creates new record with timestamps disabled', () => {
return UserNoTime
.upsert({
name: 'Young Cat'
})
.then(() => {
expect(Object.keys(self.stub.getCall(0).args[1])).to.deep.equal([
'name'
]);
});
});
it('updates all changed fields by default', () => {
return User
.upsert({
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!