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

Commit a1bf40dd by Sushant Committed by GitHub

fix(bulkCreate): dont map dataValue to fields for individualHooks:true (#9672)

1 parent da3571c9
...@@ -2553,22 +2553,6 @@ class Model { ...@@ -2553,22 +2553,6 @@ class Model {
}); });
} }
}).then(() => { }).then(() => {
for (const instance of instances) {
const values = instance.dataValues;
// set createdAt/updatedAt attributes
if (createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = now;
options.fields.indexOf(createdAtAttr) === -1 && options.fields.push(createdAtAttr);
}
if (updatedAtAttr && !values[updatedAtAttr]) {
values[updatedAtAttr] = now;
options.fields.indexOf(updatedAtAttr) === -1 && options.fields.push(updatedAtAttr);
}
instance.dataValues = Utils.mapValueFieldNames(values, options.fields, this);
}
if (options.individualHooks) { if (options.individualHooks) {
// Create each instance individually // Create each instance individually
return Promise.map(instances, instance => { return Promise.map(instances, instance => {
...@@ -2587,6 +2571,20 @@ class Model { ...@@ -2587,6 +2571,20 @@ class Model {
// Create all in one query // Create all in one query
// Recreate records from instances to represent any changes made in hooks or validation // Recreate records from instances to represent any changes made in hooks or validation
records = instances.map(instance => { records = instances.map(instance => {
const values = instance.dataValues;
// set createdAt/updatedAt attributes
if (createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = now;
options.fields.indexOf(createdAtAttr) === -1 && options.fields.push(createdAtAttr);
}
if (updatedAtAttr && !values[updatedAtAttr]) {
values[updatedAtAttr] = now;
options.fields.indexOf(updatedAtAttr) === -1 && options.fields.push(updatedAtAttr);
}
instance.dataValues = Utils.mapValueFieldNames(values, options.fields, this);
return _.omit(instance.dataValues, this._virtualAttributes); return _.omit(instance.dataValues, this._virtualAttributes);
}); });
...@@ -2613,7 +2611,6 @@ class Model { ...@@ -2613,7 +2611,6 @@ class Model {
}); });
} }
}).then(() => { }).then(() => {
// map fields back to attributes // map fields back to attributes
instances.forEach(instance => { instances.forEach(instance => {
for (const attr in this.rawAttributes) { for (const attr in this.rawAttributes) {
......
...@@ -114,6 +114,33 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -114,6 +114,33 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
it('should not map instance dataValues to fields with individualHooks: true', function() {
const User = this.sequelize.define('user', {
name: Sequelize.STRING,
type: {
type: Sequelize.STRING,
allowNull: false,
field: 'user_type'
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
field: 'created_at'
},
updatedAt: {
type: Sequelize.DATE,
field: 'modified_at'
}
});
return User.sync({force: true}).then(() => {
return User.bulkCreate([
{ name: 'James', type: 'A' },
{ name: 'Alan', type: 'Z' }
], {individualHooks: true});
});
});
it('should not insert NULL for unused fields', function() { it('should not insert NULL for unused fields', function() {
const Beer = this.sequelize.define('Beer', { const Beer = this.sequelize.define('Beer', {
style: Sequelize.STRING, style: Sequelize.STRING,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!