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

Commit aeb318ae by Shahar Hadas Committed by GitHub

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

1 parent 962ed3ca
......@@ -380,6 +380,19 @@ 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;
}
}
}
}
}
}
......
......@@ -1420,4 +1420,21 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(spy.called).to.be.ok;
});
if (current.dialect.supports.returnValues) {
it('should return default value set by the database (create)', async function() {
const User = this.sequelize.define('User', {
name: DataTypes.STRING,
code: { type: Sequelize.INTEGER, defaultValue: Sequelize.literal(2020) }
});
await User.sync({ force: true });
const user = await User.create({ name: 'FooBar' });
expect(user.name).to.be.equal('FooBar');
expect(user.code).to.be.equal(2020);
});
}
});
......@@ -577,6 +577,26 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
});
});
it('should return default value set by the database (upsert)', async function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING, primaryKey: true },
code: { type: Sequelize.INTEGER, defaultValue: Sequelize.literal(2020) }
});
await User.sync({ force: true });
const [user, created] = await User.upsert({ name: 'Test default value' }, { returning: true });
expect(user.name).to.be.equal('Test default value');
expect(user.code).to.be.equal(2020);
if (dialect === 'sqlite' || dialect === 'postgres') {
expect(created).to.be.null;
} else {
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!