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

Commit da9fdd6b by Mick Hansen

non-id/non-integer support for has many

1 parent 7fb45bf5
......@@ -42,9 +42,8 @@ module.exports = (function() {
// the id is in the source table
BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {}
, keyType = this.target.rawAttributes[this.targetIdentifier].type
newAttributes[this.identifier] = { type: this.options.keyType || keyType }
newAttributes[this.identifier] = { type: this.options.keyType || this.target.rawAttributes[this.targetIdentifier].type }
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.target, this.source, this.options)
Utils._.defaults(this.source.rawAttributes, newAttributes)
......
......@@ -137,14 +137,20 @@ module.exports = (function() {
, self = this
, primaryKeyDeleted = false
this.identifier = this.options.foreignKey || Utils._.camelizeIf(Utils.singularize(this.source.name, this.source.options.language) + "_id", !this.source.options.underscored)
this.identifier = this.options.foreignKey || Utils._.camelizeIf(
[Utils.singularize(this.source.name, this.source.options.language), this.source.primaryKeyAttribute].join("_"),
!this.source.options.underscored
)
// is there already a single sided association between the source and the target?
// or is the association on the model itself?
if ((this.isSelfAssociation && Object(this.through) === this.through) || doubleLinked) {
// remove the obsolete association identifier from the source
if (this.isSelfAssociation) {
this.foreignIdentifier = Utils._.camelizeIf((this.options.as || this.target.name) + '_id', !this.target.options.underscored)
this.foreignIdentifier = Utils._.camelizeIf(
[(this.options.as || this.target.name), this.target.primaryKeyAttribute].join('_'),
!this.target.options.underscored
)
} else {
this.foreignIdentifier = this.targetAssociation.identifier
this.targetAssociation.foreignIdentifier = this.identifier
......@@ -168,10 +174,8 @@ module.exports = (function() {
// define a new model, which connects the models
var combinedTableAttributes = {}
var sourceKeys = Object.keys(this.source.primaryKeys);
var sourceKeyType = ((!this.source.hasPrimaryKeys || sourceKeys.length !== 1) ? DataTypes.INTEGER : this.source.rawAttributes[sourceKeys[0]].type)
var targetKeys = Object.keys(this.target.primaryKeys);
var targetKeyType = ((!this.target.hasPrimaryKeys || targetKeys.length !== 1) ? DataTypes.INTEGER : this.target.rawAttributes[targetKeys[0]].type)
var sourceKeyType = this.source.rawAttributes[this.source.primaryKeyAttribute].type
var targetKeyType = this.target.rawAttributes[this.target.primaryKeyAttribute].type
if (primaryKeyDeleted) {
combinedTableAttributes[this.identifier] = {type: sourceKeyType, primaryKey: true}
......@@ -190,7 +194,7 @@ module.exports = (function() {
}
} else {
var newAttributes = {}
newAttributes[this.identifier] = { type: this.options.keyType || DataTypes.INTEGER }
newAttributes[this.identifier] = { type: this.options.keyType || this.target.rawAttributes[this.target.primaryKeyAttribute].type }
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.source, this.target, this.options)
Utils._.defaults(this.target.rawAttributes, newAttributes)
}
......
......@@ -954,7 +954,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
_.each([self.UserTasks, self.UserTasks2], function (model) {
fk = Object.keys(model.options.uniqueKeys)[0]
expect(model.options.uniqueKeys[fk].fields).to.deep.equal([ 'taskId', 'userId' ])
expect(model.options.uniqueKeys[fk].fields).to.deep.equal([ 'TaskId', 'UserId' ])
})
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!