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

Commit 56d07c6d by Shahar Hadas Committed by GitHub

fix(mssql): insert/upsert operations do not return all fields (#12434)

1 parent ad1c1537
......@@ -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];
......@@ -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;
}
}
}
}
}
}
......
......@@ -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();
......
......@@ -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;
});
});
}
});
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!