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

Commit ca0cbaa3 by andrewAtPaymentRails Committed by Sushant

fix(model): bulkCreate should populate dataValues directly (#9797)

1 parent 6804e27b
...@@ -2608,7 +2608,7 @@ class Model { ...@@ -2608,7 +2608,7 @@ class Model {
if (Array.isArray(results)) { if (Array.isArray(results)) {
results.forEach((result, i) => { results.forEach((result, i) => {
if (instances[i] && !instances[i].get(this.primaryKeyAttribute)) { if (instances[i] && !instances[i].get(this.primaryKeyAttribute)) {
instances[i] && instances[i].set(this.primaryKeyAttribute, result[this.primaryKeyField], { raw: true }); instances[i].dataValues[this.primaryKeyField] = result[this.primaryKeyField];
} }
}); });
} }
...@@ -2623,7 +2623,7 @@ class Model { ...@@ -2623,7 +2623,7 @@ class Model {
instance.dataValues[this.rawAttributes[attr].field] !== undefined && instance.dataValues[this.rawAttributes[attr].field] !== undefined &&
this.rawAttributes[attr].field !== attr this.rawAttributes[attr].field !== attr
) { ) {
instance.set(attr, instance.dataValues[this.rawAttributes[attr].field]); instance.dataValues[attr] = instance.dataValues[this.rawAttributes[attr].field];
delete instance.dataValues[this.rawAttributes[attr].field]; delete instance.dataValues[this.rawAttributes[attr].field];
} }
instance._previousDataValues[attr] = instance.dataValues[attr]; instance._previousDataValues[attr] = instance.dataValues[attr];
......
...@@ -2848,13 +2848,13 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -2848,13 +2848,13 @@ describe(Support.getTestDialectTeaser('Model'), () => {
return this.sequelize.sync({force: true}); return this.sequelize.sync({force: true});
}); });
describe('bulkCreate errors', () => { describe('bulkCreate', () => {
it('should return array of errors if validate and individualHooks are true', function() { it('errors - should return array of errors if validate and individualHooks are true', function() {
const data = [{ username: null }, const data = [{ username: null },
{ username: null }, { username: null },
{ username: null }]; { username: null }];
const user = this.sequelize.define('Users', { const user = this.sequelize.define('User', {
username: { username: {
type: Sequelize.STRING, type: Sequelize.STRING,
allowNull: false, allowNull: false,
...@@ -2865,10 +2865,41 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -2865,10 +2865,41 @@ describe(Support.getTestDialectTeaser('Model'), () => {
} }
}); });
return expect(user.bulkCreate(data, { return this.sequelize.sync({force: true}).then(() => {
expect(user.bulkCreate(data, {
validate: true, validate: true,
individualHooks: true individualHooks: true
})).to.be.rejectedWith(Promise.AggregateError); })).to.be.rejectedWith(Promise.AggregateError);
}); });
}); });
it('should not use setter when renaming fields in dataValues', function() {
const user = this.sequelize.define('User', {
username: {
type: Sequelize.STRING,
allowNull: false,
field: 'data',
get() {
const val = this.getDataValue('username');
return val.substring(0, val.length - 1);
},
set(val) {
if (val.indexOf('!') > -1) {
throw new Error('val should not include a "!"');
}
this.setDataValue('username', val + '!');
}
}
});
const data = [{ username: 'jon' }];
return this.sequelize.sync({force: true}).then(() => {
return user.bulkCreate(data).then(() => {
return user.findAll().then(users1 => {
expect(users1[0].username).to.equal('jon');
});
});
});
});
});
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!