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

Commit b2a31525 by Jan Aagaard Meier

bug(bulkcreate) Do validation before mapping of field names in bulkCreate. Closes #3787

1 parent f4982c10
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
- [FIXED] Don't try to select the primary key for models without primary key [#4607](https://github.com/sequelize/sequelize/issues/4607) - [FIXED] Don't try to select the primary key for models without primary key [#4607](https://github.com/sequelize/sequelize/issues/4607)
- [FIXED] Apply `attributes` when including a scoped model. [#4625](https://github.com/sequelize/sequelize/issues/4625) - [FIXED] Apply `attributes` when including a scoped model. [#4625](https://github.com/sequelize/sequelize/issues/4625)
- [FIXED] Use bits instead of strings for mssql booleans. [#4621](https://github.com/sequelize/sequelize/pull/4621) - [FIXED] Use bits instead of strings for mssql booleans. [#4621](https://github.com/sequelize/sequelize/pull/4621)
- [FIXED] BulkCreate validation fails for properties with `field` [#3787](https://github.com/sequelize/sequelize/issues/3787)
# 3.11.0 # 3.11.0
- [INTERNALS] Updated dependencies [#4594](https://github.com/sequelize/sequelize/pull/4594) - [INTERNALS] Updated dependencies [#4594](https://github.com/sequelize/sequelize/pull/4594)
......
...@@ -2103,20 +2103,6 @@ Model.prototype.bulkCreate = function(records, options) { ...@@ -2103,20 +2103,6 @@ Model.prototype.bulkCreate = function(records, options) {
return self.runHooks('beforeBulkCreate', instances, options); return self.runHooks('beforeBulkCreate', instances, options);
} }
}).then(function() { }).then(function() {
instances.forEach(function(instance) {
var values = Utils.mapValueFieldNames(instance.dataValues, options.fields, self);
// set createdAt/updatedAt attributes
if (createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = now;
}
if (updatedAtAttr && !values[updatedAtAttr]) {
values[updatedAtAttr] = now;
}
instance.dataValues = values;
});
// Validate // Validate
if (options.validate) { if (options.validate) {
var errors = []; var errors = [];
...@@ -2143,6 +2129,20 @@ Model.prototype.bulkCreate = function(records, options) { ...@@ -2143,6 +2129,20 @@ Model.prototype.bulkCreate = function(records, options) {
}); });
} }
}).then(function() { }).then(function() {
instances.forEach(function(instance) {
var values = Utils.mapValueFieldNames(instance.dataValues, options.fields, self);
// set createdAt/updatedAt attributes
if (createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = now;
}
if (updatedAtAttr && !values[updatedAtAttr]) {
values[updatedAtAttr] = now;
}
instance.dataValues = values;
});
if (options.individualHooks) { if (options.individualHooks) {
// Create each instance individually // Create each instance individually
return Promise.map(instances, function(instance) { return Promise.map(instances, function(instance) {
......
'use strict';
/* jshint -W030, -W110 */
var chai = require('chai')
, expect = chai.expect
, sinon = require('sinon')
, Support = require(__dirname + '/../support')
, DataTypes = require('../../../lib/data-types')
, current = Support.sequelize
, Promise = current.Promise;
describe(Support.getTestDialectTeaser('Model'), function() {
describe('bulkCreate', function () {
var Model = current.define('model', {
accountId: {
type: DataTypes.INTEGER(11).UNSIGNED,
allowNull: false,
field: 'account_id'
}
}, { timestamps: false });
before(function () {
this.stub = sinon.stub(current.getQueryInterface(), 'bulkInsert', function () {
return Promise.resolve([]);
});
});
beforeEach(function () {
this.stub.reset();
});
after(function () {
this.stub.restore();
});
describe('validations', function () {
it('should not fail for renamed fields', function () {
return Model.bulkCreate([
{ accountId: 42 }
], { validate: true }).bind(this).then(function () {
expect(this.stub.getCall(0).args[1]).to.deep.equal([
{ account_id: 42, id: null }
]);
});
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!