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

Commit 5f373ddd by Sascha Depold

auto add foreignkey for self associated has one and belongs to associations

1 parent b669ea33
...@@ -6,6 +6,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) { ...@@ -6,6 +6,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
this.target = targetModel this.target = targetModel
this.options = options this.options = options
this.isSelfAssociation = (this.source.tableName == this.target.tableName) this.isSelfAssociation = (this.source.tableName == this.target.tableName)
if(this.isSelfAssociation && !this.options.foreignKey && !!this.options.as)
this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.options.as) + "Id", this.source.options.underscored)
this.associationAccessor = this.isSelfAssociation this.associationAccessor = this.isSelfAssociation
? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName) ? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName)
: this.target.tableName : this.target.tableName
......
...@@ -7,6 +7,9 @@ var HasOne = module.exports = function(srcModel, targetModel, options) { ...@@ -7,6 +7,9 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
this.options = options this.options = options
this.isSelfAssociation = (this.source.tableName == this.target.tableName) this.isSelfAssociation = (this.source.tableName == this.target.tableName)
if(this.isSelfAssociation && !this.options.foreignKey && !!this.options.as)
this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.options.as) + "Id", this.options.underscored)
this.associationAccessor = this.isSelfAssociation this.associationAccessor = this.isSelfAssociation
? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName) ? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName)
: this.target.tableName : this.target.tableName
......
...@@ -117,5 +117,12 @@ module.exports = { ...@@ -117,5 +117,12 @@ module.exports = {
assert.isDefined(p.setMother) assert.isDefined(p.setMother)
exit(function(){}) exit(function(){})
}) })
},
'it should automatically set the foreignKey if it is a self association': function() {
var num = config.rand()
var Person = sequelize.define('Person' + num, { name: Sequelize.STRING })
Person.belongsTo(Person, {as: 'Mother'})
assert.eql(Person.associations["MotherPerson"+num+"s"].options.foreignKey, 'MotherId')
} }
} }
\ No newline at end of file
...@@ -111,5 +111,12 @@ module.exports = { ...@@ -111,5 +111,12 @@ module.exports = {
assert.isDefined(p.setMother) assert.isDefined(p.setMother)
exit(function(){}) exit(function(){})
}) })
},
'it should automatically set the foreignKey if it is a self association': function() {
var num = config.rand()
var Person = sequelize.define('Person' + num, { name: Sequelize.STRING })
Person.hasOne(Person, {as: 'Mother'})
assert.eql(Person.associations["MotherPerson"+num+"s"].options.foreignKey, 'MotherId')
} }
} }
\ 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!