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

Commit 2083c9a2 by Takashi Sasaki Committed by Sushant

fix(associations): allow binary key for belongs-to-many (#11581)

1 parent 10bf0607
......@@ -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
);
}
......
......@@ -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', {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!