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

Commit 560aa480 by John Giudici III Committed by John Giudici

Allow Model.update() to handle unique constraints - Fixes #3474

1 parent 52c6d5d9
# Next # Next
- [BUG] Fix for newlines in hstore [#3383](https://github.com/sequelize/sequelize/issues/3383) - [BUG] Fix for newlines in hstore [#3383](https://github.com/sequelize/sequelize/issues/3383)
- [BUG] Fix unique key handling in Model.update [#3474](https://github.com/sequelize/sequelize/issues/3474)
- [INTERNALS] Updated dependencies. Most notably we are moving up one major version on lodash. If you are using `sequelize.Utils._`, notice that the semantics for many matching functions have changed to include a check for `hasOwnProperty` - [INTERNALS] Updated dependencies. Most notably we are moving up one major version on lodash. If you are using `sequelize.Utils._`, notice that the semantics for many matching functions have changed to include a check for `hasOwnProperty`
+ dottie@0.3.1 + dottie@0.3.1
+ inflection@1.6.0 + inflection@1.6.0
......
...@@ -607,6 +607,7 @@ module.exports = (function() { ...@@ -607,6 +607,7 @@ module.exports = (function() {
, table = Utils._.isObject(tableName) ? tableName : { tableName: tableName } , table = Utils._.isObject(tableName) ? tableName : { tableName: tableName }
, daoTable = Utils._.find(this.sequelize.daoFactoryManager.daos, { tableName: table.tableName }); , daoTable = Utils._.find(this.sequelize.daoFactoryManager.daos, { tableName: table.tableName });
daoTable.__options = daoTable.options;
return this.sequelize.query(sql, daoTable, options); return this.sequelize.query(sql, daoTable, options);
}; };
......
...@@ -317,6 +317,31 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() { ...@@ -317,6 +317,31 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}); });
}); });
}); });
it('should enforce a unque constraint', function() {
var Model = this.sequelize.define('model', {
uniqueName: { type: Sequelize.STRING, unique: true }
});
var records = [
{ uniqueName: 'unique name one' },
{ uniqueName: 'unique name two' }
];
return Model.sync({ force: true })
.then(function() {
return Model.create(records[0]);
}).then(function(instance) {
expect(instance).to.be.ok;
return Model.create(records[1]);
}).then(function(instance) {
expect(instance).to.be.ok;
return expect(Model.update(records[0], { where: { id: instance.id } })).to.be.rejected;
}).then(function(err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.errors).to.have.length(1);
expect(err.errors[0].path).to.include('uniqueName');
expect(err.errors[0].message).to.include('must be unique');
});
});
}); });
describe('#create', function() { describe('#create', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!