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

Commit e2bc07d6 by Mick Hansen

fix(belongsToMany): fix issue with 3 keys being generated when using 2 x BTM wit…

…h foreignKey but no otherKey, closes #2991
1 parent 6f8dabc7
# Next
- [BUG] Fixed regression where `{$in: []}` would result in `IN ()` rather than `IN (NULL)` [#3105](https://github.com/sequelize/sequelize/issues/3105) [#3132](https://github.com/sequelize/sequelize/issues/3132)
- [BUG] Fixed bug where 2 x `belongsToMany` with `foreignKey` but no `otherKey` defined would result in 3 keys instead of 2. [#2991](https://github.com/sequelize/sequelize/issues/2991)
# 2.0.2
- [BUG] Fixed regression with `DataTypes.ARRAY(DataTypes.STRING(length))` [#3106](https://github.com/sequelize/sequelize/issues/3106)
......
......@@ -147,8 +147,14 @@ module.exports = (function() {
}, this);
if (this.paired) {
if (this.otherKeyDefault) this.otherKey = this.paired.foreignKey;
if (this.paired.otherKeyDefault) this.paired.otherKey = this.foreignKey;
if (this.otherKeyDefault) {
this.otherKey = this.paired.foreignKey;
}
if (this.paired.otherKeyDefault) {
// If paired otherKey was inferred we should make sure to clean it up before adding a new one that matches the foreignKey
delete this.through.model.rawAttributes[this.paired.otherKey];
this.paired.otherKey = this.foreignKey;
}
}
if (typeof this.through.model === 'string') {
......
......@@ -829,7 +829,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
it('should infer otherKey from paired BTM relationship with a through model defined', function () {
var User = this.sequelize.define('User', {});
var Place = this.sequelize.define('User', {});
var UserPlace = this.sequelize.define('UserPlace', {});
var UserPlace = this.sequelize.define('UserPlace', {id: {primaryKey: true, type: DataTypes.INTEGER, autoIncrement: true}}, {timestamps: false});
var Places = User.belongsToMany(Place, { through: UserPlace, foreignKey: 'user_id' });
var Users = Place.belongsToMany(User, { through: UserPlace, foreignKey: 'place_id' });
......@@ -839,6 +839,8 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
expect(Places.otherKey).to.equal('place_id');
expect(Users.otherKey).to.equal('user_id');
expect(Object.keys(UserPlace.rawAttributes).length).to.equal(3); // Defined primary key and two foreign keys
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!