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

Commit a5d06f0f by Mick Hansen

Merge pull request #1665 from LJ1102/1.7.0

Fix foreign key type for has-many associations, retrofits 71068a11
2 parents 0ba36cde f0bddbf5
...@@ -190,7 +190,10 @@ module.exports = (function() { ...@@ -190,7 +190,10 @@ module.exports = (function() {
} }
} else { } else {
var newAttributes = {} var newAttributes = {}
newAttributes[this.identifier] = { type: this.options.keyType || DataTypes.INTEGER } , sourceKeys = Object.keys(this.source.primaryKeys)
, keyType = ((this.source.hasPrimaryKeys && sourceKeys.length === 1) ? this.source.rawAttributes[sourceKeys[0]].type : DataTypes.INTEGER)
newAttributes[this.identifier] = { type: this.options.keyType || keyType}
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.source, this.target, this.options) Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.source, this.target, this.options)
Utils._.defaults(this.target.rawAttributes, newAttributes) Utils._.defaults(this.target.rawAttributes, newAttributes)
} }
......
...@@ -1509,5 +1509,24 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -1509,5 +1509,24 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}) })
}) })
}) })
it('infers the keyType if none provided', function(done) {
var User = this.sequelize.define('User', {
id: { type: DataTypes.STRING, primaryKey: true },
username: DataTypes.STRING
})
, Task = this.sequelize.define('Task', {
title: DataTypes.STRING
})
User.hasMany(Task)
this.sequelize.sync({ force: true }).success(function() {
expect(Task.rawAttributes.UserId.type.toString())
.to.equal(DataTypes.STRING.toString())
done()
})
})
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!