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

Commit 15ce72b7 by Richard Kemp Committed by Sushant

fix(associations/belongs-to-many): catch EmptyResultError in set/add helpers for…

… rejectOnEmpty model (#9535)
1 parent 9fb32c21
......@@ -8,6 +8,7 @@ const BelongsTo = require('./belongs-to');
const HasMany = require('./has-many');
const HasOne = require('./has-one');
const AssociationError = require('../errors').AssociationError;
const EmptyResultError = require('../errors').EmptyResultError;
const Op = require('../operators');
/**
......@@ -515,7 +516,7 @@ class BelongsToMany extends Association {
where[identifier] = sourceInstance.get(sourceKey);
where = Object.assign(where, association.through.scope);
return association.through.model.findAll(_.defaults({where, raw: true}, options)).then(currentRows => {
const updateAssociations = currentRows => {
const obsoleteAssociations = [];
const promises = [];
let defaultAttributes = options.through || {};
......@@ -578,7 +579,14 @@ class BelongsToMany extends Association {
}
return Utils.Promise.all(promises);
});
};
return association.through.model.findAll(_.defaults({where, raw: true}, options))
.then(currentRows => updateAssociations(currentRows))
.catch(error => {
if (error instanceof EmptyResultError) return updateAssociations([]);
throw error;
});
}
/**
......@@ -611,7 +619,7 @@ class BelongsToMany extends Association {
_.assign(where, association.through.scope);
return association.through.model.findAll(_.defaults({where, raw: true}, options)).then(currentRows => {
const updateAssociations = currentRows => {
const promises = [];
const unassociatedObjects = [];
const changedAssociations = [];
......@@ -662,7 +670,14 @@ class BelongsToMany extends Association {
}
return Utils.Promise.all(promises);
});
};
return association.through.model.findAll(_.defaults({where, raw: true}, options))
.then(currentRows => updateAssociations(currentRows))
.catch(error => {
if (error instanceof EmptyResultError) return updateAssociations();
throw error;
});
}
/**
......
......@@ -734,6 +734,36 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
expect(commentTags).to.have.length(2);
});
});
it('should catch EmptyResultError when rejectOnEmpty is set', function() {
const User = this.sequelize.define(
'User',
{ username: DataTypes.STRING },
{ rejectOnEmpty: true }
);
const Task = this.sequelize.define(
'Task',
{ title: DataTypes.STRING }
);
User.belongsToMany(Task, { through: 'UserTasks' });
Task.belongsToMany(User, { through: 'UserTasks' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
User.create({ id: 12 }),
Task.create({ id: 50, title: 'get started' }),
Task.create({ id: 51, title: 'following up' })
]);
}).spread((user, task1, task2) => {
return user.setTasks([task1, task2]).return(user);
}).then(user => {
return user.getTasks();
}).then(userTasks => {
expect(userTasks).to.be.an('array').that.has.a.lengthOf(2);
expect(userTasks[0]).to.be.an.instanceOf(Task);
});
});
});
describe('createAssociations', () => {
......@@ -998,6 +1028,34 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
Task.belongsToMany(User, { through: 'UserTasks' });
return this.sequelize.sync({ force: true });
});
it('should catch EmptyResultError when rejectOnEmpty is set', function() {
const User = this.sequelize.define(
'User',
{ username: DataTypes.STRING },
{ rejectOnEmpty: true }
);
const Task = this.sequelize.define(
'Task',
{ title: DataTypes.STRING }
);
User.belongsToMany(Task, { through: 'UserTasks' });
Task.belongsToMany(User, { through: 'UserTasks' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
User.create({ id: 12 }),
Task.create({ id: 50, title: 'get started' })
]);
}).spread((user, task) => {
return user.addTask(task).return(user);
}).then(user => {
return user.getTasks();
}).then(tasks => {
expect(tasks[0].title).to.equal('get started');
});
});
});
describe('addMultipleAssociations', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!