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

Commit 449625f4 by Mick Hansen

Merge pull request #1411 from geekmug/milestones/2.0.0

Fix foreignKey naming bugs in belongsTo(), hasMany(), and hasOne()
2 parents a254bf9c 6231b1ca
...@@ -14,7 +14,7 @@ module.exports = (function() { ...@@ -14,7 +14,7 @@ module.exports = (function() {
this.as = this.options.as this.as = this.options.as
if (this.isSelfAssociation && !this.options.foreignKey && !!this.as) { if (this.isSelfAssociation && !this.options.foreignKey && !!this.as) {
this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.source.tableName, this.source.options.language) + "Id", this.source.options.underscored) this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.as, this.source.options.language) + "Id", this.source.options.underscored)
} }
if (this.as) { if (this.as) {
......
...@@ -137,14 +137,14 @@ module.exports = (function() { ...@@ -137,14 +137,14 @@ module.exports = (function() {
, self = this , self = this
, primaryKeyDeleted = false , primaryKeyDeleted = false
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName, this.source.options.language) + "Id", this.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName, this.source.options.language) + "Id", this.source.options.underscored)
// is there already a single sided association between the source and the target? // is there already a single sided association between the source and the target?
// or is the association on the model itself? // or is the association on the model itself?
if ((this.isSelfAssociation && Object(this.through) === this.through) || doubleLinked) { if ((this.isSelfAssociation && Object(this.through) === this.through) || doubleLinked) {
// remove the obsolete association identifier from the source // remove the obsolete association identifier from the source
if (this.isSelfAssociation) { if (this.isSelfAssociation) {
this.foreignIdentifier = Utils._.underscoredIf((this.options.as || this.target.tableName) + 'Id', this.options.underscored) this.foreignIdentifier = Utils._.underscoredIf((this.options.as || this.target.tableName) + 'Id', this.target.options.underscored)
} else { } else {
this.foreignIdentifier = this.targetAssociation.identifier this.foreignIdentifier = this.targetAssociation.identifier
this.targetAssociation.foreignIdentifier = this.identifier this.targetAssociation.foreignIdentifier = this.identifier
......
...@@ -14,7 +14,7 @@ module.exports = (function() { ...@@ -14,7 +14,7 @@ module.exports = (function() {
this.as = this.options.as this.as = this.options.as
if (this.isSelfAssociation && !this.options.foreignKey && !!this.as) { if (this.isSelfAssociation && !this.options.foreignKey && !!this.as) {
this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.as, this.target.options.language) + "Id", this.options.underscored) this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.as, this.target.options.language) + "Id", this.target.options.underscored)
} }
if (this.as) { if (this.as) {
...@@ -42,7 +42,7 @@ module.exports = (function() { ...@@ -42,7 +42,7 @@ module.exports = (function() {
, sourceKeys = Object.keys(this.source.primaryKeys) , sourceKeys = Object.keys(this.source.primaryKeys)
, keyType = ((this.source.hasPrimaryKeys && sourceKeys.length === 1) ? this.source.rawAttributes[sourceKeys[0]].type : DataTypes.INTEGER) , keyType = ((this.source.hasPrimaryKeys && sourceKeys.length === 1) ? this.source.rawAttributes[sourceKeys[0]].type : DataTypes.INTEGER)
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName, this.source.options.language) + "Id", this.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName, this.source.options.language) + "Id", this.source.options.underscored)
newAttributes[this.identifier] = { type: this.options.keyType || keyType } newAttributes[this.identifier] = { type: this.options.keyType || keyType }
Utils._.defaults(this.target.rawAttributes, newAttributes) Utils._.defaults(this.target.rawAttributes, newAttributes)
Helpers.addForeignKeyConstraints(this.target.rawAttributes[this.identifier], this.source, this.target, this.options) Helpers.addForeignKeyConstraints(this.target.rawAttributes[this.identifier], this.source, this.target, this.options)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!