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

Commit 77dbded7 by Mick Hansen

Merge pull request #1970 from LJ1102/fix-hasmany-set-custom-primary-keys

Fix bug in hasMany.set when models have custom primary keys, add unit test
2 parents 8f2092a9 9caaab44
......@@ -105,13 +105,13 @@ module.exports = (function() {
unassociatedObjects = newAssociations.filter(function (obj) {
return !Utils._.find(oldAssociations, function (old) {
return (!!obj[foreignIdentifier] && !!old[foreignIdentifier] ? obj[foreignIdentifier] === old[foreignIdentifier] : obj.id === old.id)
return (!!obj[foreignIdentifier] ? obj[foreignIdentifier] === old[foreignIdentifier] : (!!obj[targetKeys[0]] ? obj[targetKeys[0]] === old[targetKeys[0]] : obj.id === old.id));
})
})
oldAssociations.forEach(function (old) {
var newObj = Utils._.find(newAssociations, function (obj) {
return (!!obj[foreignIdentifier] && !!old[foreignIdentifier] ? obj[foreignIdentifier] === old[foreignIdentifier] : obj.id === old.id)
return (!!obj[foreignIdentifier] ? obj[foreignIdentifier] === old[foreignIdentifier] : (!!obj[targetKeys[0]] ? obj[targetKeys[0]] === old[targetKeys[0]] : obj.id === old.id));
})
if (!newObj) {
......
......@@ -680,6 +680,34 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
it("sets new associations with custom primary keys", function (done) {
var User = this.sequelize.define('User', { uid: { type:DataTypes.INTEGER, primaryKey:true, autoIncrement: true }, username: DataTypes.STRING })
, Task = this.sequelize.define('Task', { tid: { type:DataTypes.INTEGER, primaryKey:true, autoIncrement: true }, title: DataTypes.STRING })
User.hasMany(Task)
Task.hasMany(User)
User.sync({ force: true }).success(function () {
Task.sync({ force: true }).success(function () {
User.create({ username: 'foo' }).success(function (user) {
Task.create({ title: 'task' }).success(function (task) {
task.setUsers([ user ]).success(function () {
User.create({ username: 'bar' }).success(function (user2) {
user2.user_has_task = {usertitle: "Something"};
task.setUsers([ user, user2 ]).success(function () {
task.getUsers().success(function (_users) {
expect(_users).to.have.length(2)
done()
})
})
})
})
})
})
})
})
})
it("joins an association with custom primary keys", function(done) {
var Group = this.sequelize.define('group', {
group_id: {type: DataTypes.INTEGER, primaryKey: true},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!