Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bulkCreate would have problems with a disparate field list. Closes #736 #738

Merged
merged 3 commits into from
Jul 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [BUG] Fields should be escaped by quoteIdentifier for max/min functions which allows SQL reserved keywords to be used. [#719](https://github.com/sequelize/sequelize/pull/719). thanks to durango
- [BUG] Fixed bug when trying to save objects with eagerly loaded attributes [#716](https://github.com/sequelize/sequelize/pull/716). thanks to iamjochen
- [BUG] Strings for .find() should be fixed. Also added support for string primary keys to be found easily. [#737](https://github.com/sequelize/sequelize/pull/737). thanks to durango
- [BUG] bulkCreate would have problems with a disparate field list [#738](https://github.com/sequelize/sequelize/pull/738). thanks to durango
- [BUG] Fixed problems with quoteIdentifiers and {raw: false} option on raw queries [#751](https://github.com/sequelize/sequelize/pull/751). thanks to janmeier
- [FEATURE] Validate a model before it gets saved. [#601](https://github.com/sequelize/sequelize/pull/601). thanks to durango
- [FEATURE] Schematics. [#564](https://github.com/sequelize/sequelize/pull/564). thanks to durango
Expand Down
39 changes: 13 additions & 26 deletions lib/dao-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,39 +432,26 @@ module.exports = (function() {
, updatedAtAttr = self.options.underscored ? 'updated_at' : 'updatedAt'
, createdAtAttr = self.options.underscored ? 'created_at' : 'createdAt'

fields = fields || []

// we will re-create from DAOs, which may have set up default attributes
records = []
var found = false

if (fields) {
daos.forEach(function(dao) {
var values = fields.length > 0 ? {} : dao.dataValues

// Always insert updated and created time stamps
if (self.options.timestamps) {
if (fields.indexOf(updatedAtAttr) === -1) {
fields.push(updatedAtAttr)
}
fields.forEach(function(field) {
values[field] = dao.dataValues[field]
})

if (fields.indexOf(createdAtAttr) === -1) {
fields.push(createdAtAttr)
}
if (self.options.timestamps) {
values[createdAtAttr] = Utils.now()
values[updatedAtAttr] = Utils.now()
}

// Build records for the fields we know about
daos.forEach(function(dao) {
var values = {};
fields.forEach(function(field) {
values[field] = dao.dataValues[field]
})
if (self.options.timestamps) {
values[updatedAtAttr] = Utils.now()
}
records.push(values);
})

} else {
daos.forEach(function(dao) {
records.push(dao.dataValues)
})
}
records.push(values);
})

// Validate enums
records.forEach(function(values) {
Expand Down
14 changes: 14 additions & 0 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,20 @@ module.exports = (function() {
for (key in attrs) {
this.addAttribute(key, attrs[key])
}

// this.addAttributes COMPLETELY destroys the structure of our DAO due to __defineGetter__ resetting the object
// so now we have to rebuild for bulkInserts, bulkUpdates, etc.
var rebuild = {}

// Get the correct map....
Utils._.each(this.attributes, function(key) {
if (this.dataValues.hasOwnProperty(key)) {
rebuild[key] = this.dataValues[key]
}
}.bind(this))

// This allows for aliases, etc.
this.dataValues = Utils._.extend(rebuild, this.dataValues)
}

return DAO
Expand Down
2 changes: 1 addition & 1 deletion spec/associations/has-many.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() {
this.Task.create({ title: 'task2' }).success(function(task2) {
user.setTasks([task1, task2]).on('sql', spy).on('sql', _.after(2, function (sql) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow smart :)

expect(sql).toMatch("INSERT INTO")
expect(sql).toMatch("VALUES (1,1),(1,2)")
expect(sql).toMatch("VALUES (1,1),(2,1)")
})).success(function () {
expect(spy).toHaveBeenCalledTwice() // Once for SELECT, once for INSERT into
done()
Expand Down
17 changes: 17 additions & 0 deletions spec/dao-factory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,23 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})

describe('bulkCreate', function() {
it('properly handles disparate field lists', function(done) {
var self = this
, data = [{username: 'Peter', secretValue: '42' },
{username: 'Paul'},
{username: 'Steve'}]

this.User.bulkCreate(data).success(function() {
self.User.findAll({where: {username: 'Paul'}}).success(function(users) {
expect(users.length).toEqual(1)

expect(users[0].username).toEqual("Paul")
expect(users[0].secretValue).toBeNull()

done()
})
})
})

it('inserts multiple values respecting the white list', function(done) {
var self = this
Expand Down