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

Commit 74a6b90c by Mick Hansen

Merge pull request #1927 from LJ1102/master

Inject fix 2afceddc into master, add unit test
2 parents a3a47018 a8079153
...@@ -110,8 +110,8 @@ module.exports = (function() { ...@@ -110,8 +110,8 @@ module.exports = (function() {
attributes: Utils._.defaults({}, throughAttributes, defaultAttributes) attributes: Utils._.defaults({}, throughAttributes, defaultAttributes)
}; };
changedAssociation.where[self.association.identifier] = self.instance[self.association.identifier] || self.instance.id; changedAssociation.where[self.association.identifier] = self.instance[sourceKeys[0]] || self.instance.id;
changedAssociation.where[foreignIdentifier] = newObj[foreignIdentifier] || newObj.id; changedAssociation.where[foreignIdentifier] = newObj[targetKeys[0]] || newObj.id;
if (Object.keys(changedAssociation.attributes).length) { if (Object.keys(changedAssociation.attributes).length) {
changedAssociations.push(changedAssociation); changedAssociations.push(changedAssociation);
......
...@@ -338,6 +338,35 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -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() { describe('addAssociations', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!