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

Commit 26aef71d by Jan Aagaard Meier

Pass the transaction parameter to update in hasmany double linked inject adder. Closes #1968

1 parent 1f788071
......@@ -180,7 +180,7 @@ module.exports = (function() {
attributes = Utils._.defaults({}, newAssociation[targetAssociation.through.name], additionalAttributes);
if (Object.keys(attributes).length) {
return targetAssociation.through.update(attributes, where);
return targetAssociation.through.update(attributes, where, options);
} else {
return Utils.Promise.resolve();
}
......
......@@ -220,7 +220,7 @@ module.exports = (function() {
* @see {Transaction}
* @see {Sequelize#transaction}
*/
Sequelize.prototype.Transaction = Transaction;
Sequelize.prototype.Transaction = Sequelize.Transaction = Transaction;
/**
* A general error class
......
......@@ -1118,6 +1118,47 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
});
});
it('supports transactions when updating a through model', function() {
return Support.prepareTransactionTest(this.sequelize).bind({}).then(function(sequelize) {
this.User = sequelize.define('User', { username: DataTypes.STRING });
this.Task = sequelize.define('Task', { title: DataTypes.STRING });
this.UserTask = sequelize.define('UserTask', {
status: Sequelize.STRING
});
this.User.hasMany(this.Task, { through: this.UserTask });
this.Task.hasMany(this.User, { through: this.UserTask });
this.sequelize = sequelize;
return sequelize.sync({ force: true });
}).then(function() {
return Promise.all([
this.User.create({ username: 'foo' }),
this.Task.create({ title: 'task' }),
this.sequelize.transaction({ isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED })
]);
}).spread(function(user, task, t){
this.task = task;
this.user = user;
this.t = t;
return task.addUser(user, { status: 'pending' }); // Create without transaction, so the old value is accesible from outside the transaction
}).then(function() {
return this.task.addUser(this.user, { transaction: this.t, status: 'completed' }); // Add an already exisiting user in a transaction, updating a value in the join table
}).then(function(hasUser) {
return Promise.all([
this.user.getTasks(),
this.user.getTasks({ transaction: this.t })
]);
}).spread(function(tasks, transactionTasks) {
expect(tasks[0].UserTask.status).to.equal('pending');
expect(transactionTasks[0].UserTask.status).to.equal('completed');
return this.t.rollback();
});
});
it('supports passing the primary key instead of an object', function () {
var User = this.sequelize.define('User', { username: DataTypes.STRING })
, Task = this.sequelize.define('Task', { title: DataTypes.STRING });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!