Skip to content

Commit

Permalink
fix(associations): allow binary key for belongs-to-many (#11581)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsasaki609 authored and sushantdhiman committed Oct 19, 2019
1 parent 10bf060 commit 2083c9a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/associations/belongs-to-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ class BelongsToMany extends Association {
};

return this.get(sourceInstance, options).then(associatedObjects =>
_.differenceBy(instancePrimaryKeys, associatedObjects, this.targetKey).length === 0
_.differenceWith(instancePrimaryKeys, associatedObjects,
(a, b) => _.isEqual(a[this.targetKey], b[this.targetKey])).length === 0
);
}

Expand Down
54 changes: 54 additions & 0 deletions test/integration/associations/belongs-to-many.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,60 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
});
});

describe('hasAssociations with binary key', () => {
beforeEach(function() {
const keyDataType = dialect === 'mysql' || dialect === 'mariadb' ? 'BINARY(255)' : DataTypes.BLOB('tiny');
this.Article = this.sequelize.define('Article', {
id: {
type: keyDataType,
primaryKey: true
}
});
this.Label = this.sequelize.define('Label', {
id: {
type: keyDataType,
primaryKey: true
}
});
this.ArticleLabel = this.sequelize.define('ArticleLabel');

this.Article.belongsToMany(this.Label, { through: this.ArticleLabel });
this.Label.belongsToMany(this.Article, { through: this.ArticleLabel });

return this.sequelize.sync({ force: true });
});

it('answers true for labels that have been assigned', function() {
return Promise.all([
this.Article.create({
id: Buffer.alloc(255)
}),
this.Label.create({
id: Buffer.alloc(255)
})
]).then(([article, label]) => Promise.all([
article,
label,
article.addLabel(label, {
through: 'ArticleLabel'
})
])).then(([article, label]) => article.hasLabels([label]))
.then(result => expect(result).to.be.true);
});

it('answer false for labels that have not been assigned', function() {
return Promise.all([
this.Article.create({
id: Buffer.alloc(255)
}),
this.Label.create({
id: Buffer.alloc(255)
})
]).then(([article, label]) => article.hasLabels([label]))
.then(result => expect(result).to.be.false);
});
});

describe('countAssociations', () => {
beforeEach(function() {
this.User = this.sequelize.define('User', {
Expand Down

0 comments on commit 2083c9a

Please sign in to comment.