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

Commit 5eef5803 by Mick Hansen

fix(Model): instances returned from bulkCreate now have primary key values if a …

…dialect that supports RETURNING (pg)
1 parent a2ae2b9b
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
- [FEATURE] `instance.update()` using default fields will now automatically also save values provided via `beforeUpdate` hooks - [FEATURE] `instance.update()` using default fields will now automatically also save values provided via `beforeUpdate` hooks
- [BUG] Fixed bad SQL when updating a JSON attribute with a different `field` - [BUG] Fixed bad SQL when updating a JSON attribute with a different `field`
- [BUG] Fixed issue with creating and updating values of a `DataTypes.ARRAY(DataTypes.JSON)` attribute - [BUG] Fixed issue with creating and updating values of a `DataTypes.ARRAY(DataTypes.JSON)` attribute
- [BUG] `Model.bulkCreate([{}], {returning: true})` will now correctly result in instances with primary key values.
#### Backwards compatability changes #### Backwards compatability changes
- `instance.update()` using default fields will now automatically also save values provided via `beforeUpdate` hooks - `instance.update()` using default fields will now automatically also save values provided via `beforeUpdate` hooks
......
...@@ -233,7 +233,7 @@ module.exports = (function() { ...@@ -233,7 +233,7 @@ module.exports = (function() {
} }
} }
return self.callee || (rows && rows[0]) || undefined; return self.callee || (rows && ((self.options.plain && rows[0]) || rows)) || undefined;
} else if (self.isVersionQuery()) { } else if (self.isVersionQuery()) {
return results[0].version; return results[0].version;
} else { } else {
......
...@@ -1378,7 +1378,11 @@ module.exports = (function() { ...@@ -1378,7 +1378,11 @@ module.exports = (function() {
} }
// Insert all records at once // Insert all records at once
return self.QueryInterface.bulkInsert(self.getTableName(options), records, options, attributes); return self.QueryInterface.bulkInsert(self.getTableName(options), records, options, attributes).then(function (results) {
return results.forEach(function (result, i) {
daos[i].set(self.primaryKeyAttribute, result[self.rawAttributes[self.primaryKeyAttribute].field], {raw: true});
});
});
} }
}).then(function() { }).then(function() {
// Run after hook // Run after hook
......
...@@ -345,6 +345,53 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -345,6 +345,53 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
} }
if (current.dialect.supports['RETURNING']) {
describe('Autoincrement values', function () {
it('should make the autoincremented values available on the returned instances', function () {
var User = this.sequelize.define('user', {});
return User.sync({force: true}).then(function () {
return User.bulkCreate([
{},
{},
{}
], {returning: true}).then(function (users) {
expect(users.length).to.be.ok;
users.forEach(function (user, i) {
expect(user.get('id')).to.be.ok;
expect(user.get('id')).to.equal(i+1);
});
});
});
});
it('should make the autoincremented values available on the returned instances', function () {
var User = this.sequelize.define('user', {
maId: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
field: 'yo_id'
}
});
return User.sync({force: true}).then(function () {
return User.bulkCreate([
{},
{},
{}
], {returning: true}).then(function (users) {
expect(users.length).to.be.ok;
users.forEach(function (user, i) {
expect(user.get('maId')).to.be.ok;
expect(user.get('maId')).to.equal(i+1);
});
});
});
});
});
}
it('is possible to use casting when creating an instance', function(done) { it('is possible to use casting when creating an instance', function(done) {
var self = this var self = this
, type = Support.dialectIsMySQL() ? 'signed' : 'integer' , type = Support.dialectIsMySQL() ? 'signed' : 'integer'
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!