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

Commit 5ff5f618 by Mick Hansen

use Model.name and not Model.tableName for association aliases and foreignkeys

1 parent d480d382
...@@ -10,7 +10,7 @@ module.exports = (function() { ...@@ -10,7 +10,7 @@ module.exports = (function() {
this.target = target this.target = target
this.options = options this.options = options
this.isSingleAssociation = true this.isSingleAssociation = true
this.isSelfAssociation = (this.source.tableName == this.target.tableName) this.isSelfAssociation = (this.source.name == this.target.name)
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) {
...@@ -20,15 +20,11 @@ module.exports = (function() { ...@@ -20,15 +20,11 @@ module.exports = (function() {
if (this.as) { if (this.as) {
this.isAliased = true this.isAliased = true
} else { } else {
this.as = Utils.singularize(this.target.tableName, this.target.options.language)
// Hotfix
if (this.as === this.target.tableName) {
this.as = Utils.singularize(this.target.name, this.target.options.language) this.as = Utils.singularize(this.target.name, this.target.options.language)
} }
}
this.associationAccessor = this.isSelfAssociation this.associationAccessor = this.isSelfAssociation
? Utils.combineTableNames(this.target.tableName, this.as) ? Utils.combineTableNames(this.target.name, this.as)
: this.as : this.as
this.options.useHooks = options.useHooks this.options.useHooks = options.useHooks
...@@ -46,7 +42,7 @@ module.exports = (function() { ...@@ -46,7 +42,7 @@ module.exports = (function() {
, targetKeys = Object.keys(this.target.primaryKeys) , targetKeys = Object.keys(this.target.primaryKeys)
, keyType = ((this.target.hasPrimaryKeys && targetKeys.length === 1) ? this.target.rawAttributes[targetKeys[0]].type : DataTypes.INTEGER) , keyType = ((this.target.hasPrimaryKeys && targetKeys.length === 1) ? this.target.rawAttributes[targetKeys[0]].type : DataTypes.INTEGER)
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName, this.target.options.language) + "Id", this.source.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.name, this.target.options.language) + "Id", this.source.options.underscored)
newAttributes[this.identifier] = { type: this.options.keyType || keyType } newAttributes[this.identifier] = { type: this.options.keyType || keyType }
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.target, this.source, this.options) Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.target, this.source, this.options)
......
...@@ -116,7 +116,7 @@ module.exports = (function() { ...@@ -116,7 +116,7 @@ module.exports = (function() {
if (this.as) { if (this.as) {
this.isAliased = true this.isAliased = true
} else { } else {
this.as = Utils.pluralize(this.target.tableName, this.target.options.language) this.as = Utils.pluralize(this.target.name, this.target.options.language)
} }
this.accessors = { this.accessors = {
...@@ -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.source.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.name, 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.target.options.underscored) this.foreignIdentifier = Utils._.underscoredIf((this.options.as || this.target.name) + '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
......
...@@ -10,7 +10,7 @@ module.exports = (function() { ...@@ -10,7 +10,7 @@ module.exports = (function() {
this.target = targetDAO this.target = targetDAO
this.options = options this.options = options
this.isSingleAssociation = true this.isSingleAssociation = true
this.isSelfAssociation = (this.source.tableName == this.target.tableName) this.isSelfAssociation = (this.source.name == this.target.name)
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) {
...@@ -20,11 +20,11 @@ module.exports = (function() { ...@@ -20,11 +20,11 @@ module.exports = (function() {
if (this.as) { if (this.as) {
this.isAliased = true this.isAliased = true
} else { } else {
this.as = Utils.singularize(this.target.tableName, this.target.options.language) this.as = Utils.singularize(this.target.name, this.target.options.language)
} }
this.associationAccessor = this.isSelfAssociation this.associationAccessor = this.isSelfAssociation
? Utils.combineTableNames(this.target.tableName, this.as) ? Utils.combineTableNames(this.target.name, this.as)
: this.as : this.as
this.options.useHooks = options.useHooks this.options.useHooks = options.useHooks
...@@ -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.source.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.name, 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!