Skip to content

Commit

Permalink
Merge pull request #1927 from LJ1102/master
Browse files Browse the repository at this point in the history
Inject fix 2afcedd into master, add unit test
  • Loading branch information
mickhansen committed Jun 19, 2014
2 parents a3a4701 + a807915 commit 74a6b90
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/associations/has-many-double-linked.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ module.exports = (function() {
attributes: Utils._.defaults({}, throughAttributes, defaultAttributes)
};

changedAssociation.where[self.association.identifier] = self.instance[self.association.identifier] || self.instance.id;
changedAssociation.where[foreignIdentifier] = newObj[foreignIdentifier] || newObj.id;
changedAssociation.where[self.association.identifier] = self.instance[sourceKeys[0]] || self.instance.id;
changedAssociation.where[foreignIdentifier] = newObj[targetKeys[0]] || newObj.id;

if (Object.keys(changedAssociation.attributes).length) {
changedAssociations.push(changedAssociation);
Expand Down
29 changes: 29 additions & 0 deletions test/associations/has-many.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,35 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})

it('allows to update join table attributes with primary keys other than "id"', function () {
var Article = this.sequelize.define('Article', {aid: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true}, title: DataTypes.STRING})
, Label = this.sequelize.define('Label', {lid: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true}, text: DataTypes.STRING})
, ArticleHasLabel = this.sequelize.define('ArticleHasLabel', {color: DataTypes.ENUM(["red", "green", "blue"])})

Article.hasMany(Label, {through: ArticleHasLabel})
Label.hasMany(Article, {through: ArticleHasLabel})

return this.sequelize.sync({ force: true }).then(function () {
return Article.create().then(function (article) {
return Label.create({ text: "cheap" }).then(function (label1) {
return Label.create({ text: "steal" }).then(function (label2) {
label2.ArticleHasLabel = {color: "green"}
return article.setLabels([label1, label2]).then(function () {
label1.ArticleHasLabel = {color: "red"}
article.setLabels([label1, label2]).then(function () {
article.getLabels().then(function (labels) {
expect(labels).to.have.length(2)
expect(labels[0].ArticleHasLabel.color).to.equal("red")
expect(labels[1].ArticleHasLabel.color).to.equal("green")
})
})
})
})
})
})
})
})
})

describe('addAssociations', function() {
Expand Down

0 comments on commit 74a6b90

Please sign in to comment.