Skip to content

Commit

Permalink
fix(mssql): insert/upsert operations do not return all fields (#12434)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaharHD committed Jun 27, 2020
1 parent ad1c153 commit 56d07c6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/dialects/mssql/query.js
Expand Up @@ -204,7 +204,8 @@ class Query extends AbstractQuery {
return this.handleShowIndexesQuery(data);
}
if (this.isUpsertQuery()) {
return data[0];
this.handleInsertQuery(data);
return this.instance || data[0];
}
if (this.isCallQuery()) {
return data[0];
Expand Down Expand Up @@ -391,6 +392,18 @@ class Query extends AbstractQuery {
id = id || autoIncrementAttributeAlias && results && results[0][autoIncrementAttributeAlias];

this.instance[autoIncrementAttribute] = id;

if (this.instance.dataValues) {
for (const key in results[0]) {
if (Object.prototype.hasOwnProperty.call(results[0], key)) {
const record = results[0][key];

const attr = _.find(this.model.rawAttributes, attribute => attribute.fieldName === key || attribute.field === key);

this.instance.dataValues[attr && attr.fieldName || key] = record;
}
}
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/integration/model/create.test.js
Expand Up @@ -1454,6 +1454,23 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});

if (current.dialect.supports.returnValues) {
it('should return default value set by the database (create)', function() {

const User = this.sequelize.define('User', {
name: DataTypes.STRING,
code: { type: Sequelize.INTEGER, defaultValue: Sequelize.literal(2020) }
});

return User.sync({ force: true })
.then(() => User.create({ name: 'FooBar' }))
.then(user => {
expect(user.name).to.be.equal('FooBar');
expect(user.code).to.be.equal(2020);
});
});
}

it('should support logging', function() {
const spy = sinon.spy();

Expand Down
15 changes: 15 additions & 0 deletions test/integration/model/upsert.test.js
Expand Up @@ -620,6 +620,21 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});

it('should return default value set by the database (upsert)', function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING, primaryKey: true },
code: { type: Sequelize.INTEGER, defaultValue: Sequelize.literal(2020) }
});

return User.sync({ force: true })
.then(() => User.upsert({ name: 'Test default value' }, { returning: true }))
.then(([user, created]) => {
expect(user.name).to.be.equal('Test default value');
expect(user.code).to.be.equal(2020);
expect(created).to.be.true;
});
});
}
});
}
Expand Down

0 comments on commit 56d07c6

Please sign in to comment.