Skip to content

Commit

Permalink
Merge pull request #3730 from BridgeAR/fix-crash
Browse files Browse the repository at this point in the history
Fix sqlite crash while parsing unique constraint errors. Fixes #3546 #3409
  • Loading branch information
mickhansen committed May 16, 2015
2 parents 2161b3f + 83558e4 commit 7c9d1d7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Next
- [BUG] fix showIndexQuery so appropriate indexes are returned when a schema is used
- [BUG] Fix addIndexQuery error when the model has a schema
- [BUG] Fix app crash in sqlite while running in special unique constraint errors [3730](https://github.com/sequelize/sequelize/pull/3730)

# 2.1.3
- [BUG] Fix regression introduced in 2.1.2: updatedAt not set anymore [3667](https://github.com/sequelize/sequelize/pull/3667)
Expand Down
16 changes: 9 additions & 7 deletions lib/dialects/sqlite/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,17 @@ module.exports = (function() {

fields.forEach(function(field) {
errors.push(new sequelizeErrors.ValidationErrorItem(
field + ' must be unique', 'unique violation', field, self.callee[field]));
field + ' must be unique', 'unique violation', field, self.callee && self.callee[field]));
});

Utils._.forOwn(this.callee.__options.uniqueKeys, function(constraint) {
if (Utils._.isEqual(constraint.fields, fields) && !!constraint.msg) {
message = constraint.msg;
return false;
}
});
if (this.callee) {
Utils._.forOwn(this.callee.__options.uniqueKeys, function(constraint) {
if (Utils._.isEqual(constraint.fields, fields) && !!constraint.msg) {
message = constraint.msg;
return false;
}
});
}

return new sequelizeErrors.UniqueConstraintError({
message: message,
Expand Down
14 changes: 13 additions & 1 deletion test/integration/dialects/sqlite/dao.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,17 @@ if (dialect === 'sqlite') {
});
});
});

describe('regression tests', function() {

it('do not crash while parsing unique constraint errors', function() {
var Payments = this.sequelize.define('payments', {});

return Payments.sync({force: true}).then(function () {
return (expect(Payments.bulkCreate([{id: 1}, {id: 1}], { ignoreDuplicates: false })).to.eventually.be.rejected);
});

});
});
});
}
}

0 comments on commit 7c9d1d7

Please sign in to comment.