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

Commit b9d42fc9 by Mick Hansen

fix(model): findOrCreate should not include postgres exception catcher in result, closes #3246

1 parent 7a73493f
...@@ -135,11 +135,18 @@ module.exports = (function() { ...@@ -135,11 +135,18 @@ module.exports = (function() {
return results; return results;
} }
if (rows[0] && rows[0].sequelize_caught_exception) { if (rows[0] && rows[0].sequelize_caught_exception !== undefined) {
throw new self.formatError({ if (rows[0].sequelize_caught_exception !== null) {
code: '23505', throw new self.formatError({
detail: rows[0].sequelize_caught_exception code: '23505',
}); detail: rows[0].sequelize_caught_exception
});
} else {
rows = rows.map(function (row) {
delete row.sequelize_caught_exception;
return row;
});
}
} }
if (self.isShowIndexesQuery()) { if (self.isShowIndexesQuery()) {
......
...@@ -89,19 +89,14 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -89,19 +89,14 @@ describe(Support.getTestDialectTeaser('Model'), function() {
username: 'gottlieb' username: 'gottlieb'
}); });
}).then(function () { }).then(function () {
return User.findOrCreate({ return expect(User.findOrCreate({
where: { where: {
objectId: 'asdasdasd' objectId: 'asdasdasd'
}, },
defaults: { defaults: {
username: 'gottlieb' username: 'gottlieb'
} }
}).then(function () { })).to.eventually.be.rejectedWith(Sequelize.UniqueConstraintError);
throw new Error('I should have been rejected');
}).catch(function (err) {
expect(err instanceof Sequelize.UniqueConstraintError).to.be.ok;
expect(err.fields).to.be.ok;
});
}); });
}); });
...@@ -266,6 +261,28 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -266,6 +261,28 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('does not include exception catcher in response', function() {
var self = this
, data = {
username: 'Username',
data: 'ThisIsData'
};
return self.User.findOrCreate({
where: data,
defaults: {}
}).spread(function(user, created) {
expect(user.dataValues.sequelize_caught_exception).to.be.undefined;
}).then(function () {
return self.User.findOrCreate({
where: data,
defaults: {}
}).spread(function(user, created) {
expect(user.dataValues.sequelize_caught_exception).to.be.undefined;
});
});
});
it('creates new instance with default value.', function() { it('creates new instance with default value.', function() {
var data = { var data = {
username: 'Username' username: 'Username'
......
...@@ -7,8 +7,9 @@ var fs = require('fs') ...@@ -7,8 +7,9 @@ var fs = require('fs')
, DataTypes = require(__dirname + '/../lib/data-types') , DataTypes = require(__dirname + '/../lib/data-types')
, Config = require(__dirname + '/config/config') , Config = require(__dirname + '/config/config')
, chai = require('chai') , chai = require('chai')
, expect = chai.expect , expect = chai.expect;
, chaiAsPromised = require('chai-as-promised');
chai.use(require('chai-as-promised'));
// Make sure errors get thrown when testing // Make sure errors get thrown when testing
Sequelize.Promise.onPossiblyUnhandledRejection(function(e, promise) { Sequelize.Promise.onPossiblyUnhandledRejection(function(e, promise) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!