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

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() {
return results;
}
if (rows[0] && rows[0].sequelize_caught_exception) {
throw new self.formatError({
code: '23505',
detail: rows[0].sequelize_caught_exception
});
if (rows[0] && rows[0].sequelize_caught_exception !== undefined) {
if (rows[0].sequelize_caught_exception !== null) {
throw new self.formatError({
code: '23505',
detail: rows[0].sequelize_caught_exception
});
} else {
rows = rows.map(function (row) {
delete row.sequelize_caught_exception;
return row;
});
}
}
if (self.isShowIndexesQuery()) {
......
......@@ -89,19 +89,14 @@ describe(Support.getTestDialectTeaser('Model'), function() {
username: 'gottlieb'
});
}).then(function () {
return User.findOrCreate({
return expect(User.findOrCreate({
where: {
objectId: 'asdasdasd'
},
defaults: {
username: 'gottlieb'
}
}).then(function () {
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;
});
})).to.eventually.be.rejectedWith(Sequelize.UniqueConstraintError);
});
});
......@@ -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() {
var data = {
username: 'Username'
......
......@@ -7,8 +7,9 @@ var fs = require('fs')
, DataTypes = require(__dirname + '/../lib/data-types')
, Config = require(__dirname + '/config/config')
, chai = require('chai')
, expect = chai.expect
, chaiAsPromised = require('chai-as-promised');
, expect = chai.expect;
chai.use(require('chai-as-promised'));
// Make sure errors get thrown when testing
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!