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

Commit 4f8dc717 by Jan Aagaard Meier

🐛 (unique constraint violation) Check for model and unique keys being define…

…d in unique constraint parsing
1 parent 2629f3e1
# Next
- [FEATURE] Geometry support for postgres
- [BUG] Fix wrong count for `findAndCountAll` with required includes [#4016](https://github.com/sequelize/sequelize/pull/4016)
- [BUG] Fix problems related to parsing of unique constraint errors [#4017](https://github.com/sequelize/sequelize/issues/4017) and [#4012](https://github.com/sequelize/sequelize/issues/4012)
# 3.3.2
- [FIXED] upsert no longer updates with default values each time [#3994](https://github.com/sequelize/sequelize/pull/3994)
......
......@@ -417,13 +417,16 @@ AbstractQuery.prototype.findTableNameInAttribute = function(attribute) {
AbstractQuery.prototype.getUniqueConstraintErrorMessage = function(field) {
var message = field + ' must be unique';
var self = this;
Object.keys(self.model.uniqueKeys).forEach(function(key) {
if (self.model.uniqueKeys[key].fields.indexOf(field.replace(/"/g, '')) >= 0) {
if (self.model.uniqueKeys[key].hasOwnProperty('msg')) {
message = self.model.uniqueKeys[key].msg;
if (self.model) {
Object.keys(self.model.uniqueKeys).forEach(function(key) {
if (self.model.uniqueKeys[key].fields.indexOf(field.replace(/"/g, '')) >= 0) {
if (self.model.uniqueKeys[key].hasOwnProperty('msg')) {
message = self.model.uniqueKeys[key].msg;
}
}
}
});
});
}
return message;
};
......
......@@ -172,9 +172,11 @@ Query.prototype.formatError = function (err) {
if (match && match.length > 1) {
var fields = {}
, message = 'Validation error'
, uniqueKey = this.model.uniqueKeys[match[1]];
, uniqueKey = this.model && this.model.uniqueKeys[match[1]];
if (!!uniqueKey.msg) message = uniqueKey.msg;
if (uniqueKey && !!uniqueKey.msg) {
message = uniqueKey.msg;
}
if (!!match[2]) {
var values = match[2].split(',').map(Function.prototype.call, String.prototype.trim);
if (!!uniqueKey) {
......
......@@ -220,5 +220,29 @@ describe(Support.getTestDialectTeaser('Sequelize Errors'), function () {
expect(spy).to.have.been.calledOnce;
});
});
it('Works when unique keys are not defined in sequelize', function () {
var User = this.sequelize.define('user', {
name: {
type: Sequelize.STRING,
unique: 'unique \n unique',
}
}, { timestamps: false });
return this.sequelize.sync({ force: true }).bind(this).then(function () {
// Now let's pretend the index was created by someone else, and sequelize doesn't know about it
User = this.sequelize.define('user', {
name: Sequelize.STRING
}, { timestamps: false });
return User.create({ name: 'jan' });
}).then(function () {
// It should work even though the unique key is not defined in the model
return expect(User.create({ name: 'jan' })).to.be.rejectedWith(this.sequelize.UniqueConstraintError);
}).then(function () {
// And when the model is not passed at all
return expect(this.sequelize.query('INSERT INTO users (name) VALUES (\'jan\')')).to.be.rejectedWith(this.sequelize.UniqueConstraintError);
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!