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

Commit b669ea33 by Sascha Depold

fixed self association with belongs-to

1 parent dabdcbe5
...@@ -5,6 +5,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) { ...@@ -5,6 +5,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
this.source = srcModel this.source = srcModel
this.target = targetModel this.target = targetModel
this.options = options this.options = options
this.isSelfAssociation = (this.source.tableName == this.target.tableName)
this.associationAccessor = this.isSelfAssociation
? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName)
: this.target.tableName
} }
// the id is in the source table // the id is in the source table
......
...@@ -15,7 +15,7 @@ var Associations = module.exports = { ...@@ -15,7 +15,7 @@ var Associations = module.exports = {
// the id is in this table // the id is in this table
var BelongsTo = require("./belongs-to") var BelongsTo = require("./belongs-to")
var association = new BelongsTo(this, associatedModel, Utils._.extend((options||{}), this.options)) var association = new BelongsTo(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[associatedModel.tableName] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
}, },
hasMany: function(associatedModel, options) { hasMany: function(associatedModel, options) {
// the id is in the foreign table or in a connecting table // the id is in the foreign table or in a connecting table
......
...@@ -104,5 +104,18 @@ module.exports = { ...@@ -104,5 +104,18 @@ module.exports = {
}) })
}) })
}) })
},
'it should correctly handle self associations': function(exit) {
var Person = sequelize.define('Person' + config.rand(), { name: Sequelize.STRING })
Person.belongsTo(Person, {as: 'Mother', foreignKey: 'MotherId'})
Person.belongsTo(Person, {as: 'Father', foreignKey: 'FatherId'})
Person.sync({force: true}).on('success', function() {
var p = Person.build()
assert.isDefined(p.setFather)
assert.isDefined(p.setMother)
exit(function(){})
})
} }
} }
\ No newline at end of file
...@@ -101,6 +101,15 @@ module.exports = { ...@@ -101,6 +101,15 @@ module.exports = {
}, },
'it should correctly associate with itself': function(exit) { 'it should correctly associate with itself': function(exit) {
var Person = sequelize.define('Person' + config.rand(), { name: Sequelize.STRING }) var Person = sequelize.define('Person' + config.rand(), { name: Sequelize.STRING })
Person.hasOne(Person, {as: 'Mother', foreignKey: 'MotherId'})
Person.hasOne(Person, {as: 'Father', foreignKey: 'FatherId'})
Person.sync({force: true}).on('success', function() {
var p = Person.build()
assert.isDefined(p.setFather)
assert.isDefined(p.setMother)
exit(function(){}) exit(function(){})
})
} }
} }
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!