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

Commit 751da171 by Mick Hansen

Do not pass hooks option on to through model, fixes #1263

1 parent fea739d8
...@@ -67,28 +67,28 @@ module.exports = (function() { ...@@ -67,28 +67,28 @@ module.exports = (function() {
* Else find partner DAOFactory if present, to identify double linked association * Else find partner DAOFactory if present, to identify double linked association
*/ */
else if (this.through) { else if (this.through) {
_.each(this.target.associations, function (association, accessor) { _.each(this.target.associations, function (association, accessor) {
if (self.source === association.target) { if (self.source === association.target) {
var paired = false var paired = false
// If through is default, we determine pairing by the accesor value (i.e. DAOFactory's using as won't pair, but regular ones will) // If through is default, we determine pairing by the accesor value (i.e. DAOFactory's using as won't pair, but regular ones will)
if (self.through === true && accessor === self.associationAccessor) { if (self.through === true && accessor === self.associationAccessor) {
paired = true paired = true
} }
// If through is not default, determine pairing by through value (model/string) // If through is not default, determine pairing by through value (model/string)
if (self.through !== true && self.options.through === association.options.through) { if (self.through !== true && self.options.through === association.options.through) {
paired = true paired = true
} }
// If paired, set properties identifying both associations as double linked, and allow them to each eachtoerh // If paired, set properties identifying both associations as double linked, and allow them to each eachtoerh
if (paired) { if (paired) {
self.doubleLinked = true self.doubleLinked = true
association.doubleLinked = true association.doubleLinked = true
self.targetAssociation = association self.targetAssociation = association
association.targetAssociation = self association.targetAssociation = self
}
} }
} })
})
} }
/* /*
...@@ -105,7 +105,9 @@ module.exports = (function() { ...@@ -105,7 +105,9 @@ module.exports = (function() {
tableName: this.through tableName: this.through
})) }))
if (this.targetAssociation) this.targetAssociation.through = this.through if (this.targetAssociation) {
this.targetAssociation.through = this.through
}
} }
this.options.tableName = this.combinedName = (this.through === Object(this.through) ? this.through.tableName : this.through) this.options.tableName = this.combinedName = (this.through === Object(this.through) ? this.through.tableName : this.through)
......
...@@ -13,7 +13,7 @@ Mixin.hasOne = function(associatedDAOFactory, options) { ...@@ -13,7 +13,7 @@ Mixin.hasOne = function(associatedDAOFactory, options) {
options.useHooks = options.hooks options.useHooks = options.hooks
// the id is in the foreign table // the id is in the foreign table
var association = new HasOne(this, associatedDAOFactory, Utils._.extend((options||{}), this.options)) var association = new HasOne(this, associatedDAOFactory, Utils._.extend(options, this.options))
this.associations[association.associationAccessor] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
association.injectGetter(this.DAO.prototype); association.injectGetter(this.DAO.prototype);
...@@ -46,8 +46,10 @@ Mixin.hasMany = function(associatedDAOFactory, options) { ...@@ -46,8 +46,10 @@ Mixin.hasMany = function(associatedDAOFactory, options) {
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks) options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
options.useHooks = options.hooks options.useHooks = options.hooks
options = Utils._.extend(options, Utils._.omit(this.options, ['hooks']))
// the id is in the foreign table or in a connecting table // the id is in the foreign table or in a connecting table
var association = new HasMany(this, associatedDAOFactory, Utils._.extend((options||{}), this.options)) var association = new HasMany(this, associatedDAOFactory, options)
this.associations[association.associationAccessor] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
association.injectGetter(this.DAO.prototype) association.injectGetter(this.DAO.prototype)
......
...@@ -4,6 +4,8 @@ var chai = require('chai') ...@@ -4,6 +4,8 @@ var chai = require('chai')
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, DataTypes = require(__dirname + '/../lib/data-types') , DataTypes = require(__dirname + '/../lib/data-types')
, _ = require('lodash') , _ = require('lodash')
, Sequelize = Support.Sequelize
, sinon = require('sinon')
chai.Assertion.includeStack = true chai.Assertion.includeStack = true
...@@ -1866,6 +1868,43 @@ describe(Support.getTestDialectTeaser("Hooks"), function () { ...@@ -1866,6 +1868,43 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
}) })
}) })
}) })
it('should not trigger hooks on parent when using N:M association setters', function (done) {
var A = this.sequelize.define('A', {
name: Sequelize.STRING
})
var B = this.sequelize.define('B', {
name: Sequelize.STRING
})
var hookCalled = 0
A.addHook('afterCreate', function (instance, next) {
hookCalled++
next()
})
B.hasMany(A)
A.hasMany(B)
this.sequelize.sync({force: true}).done(function (err) {
expect(err).not.to.be.ok
var chainer = new Sequelize.Utils.QueryChainer([
A.create({name: 'a'}),
B.create({name: 'b'})
])
chainer.run().done(function (err, res, a, b) {
expect(err).not.to.be.ok
a.addB(b).done(function (err) {
expect(err).not.to.be.ok
expect(hookCalled).to.equal(1)
done()
})
})
})
})
}) })
describe('#updateAttributes', function() { describe('#updateAttributes', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!