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

Commit 77fd8baf by Mick Hansen

fix(belongs-to-many): association.through should be cloned from association.opti…

…ons.through, not a reference
1 parent 08fcb5ce
...@@ -54,7 +54,7 @@ var BelongsToMany = function(source, target, options) { ...@@ -54,7 +54,7 @@ var BelongsToMany = function(source, target, options) {
this.targetAssociation = null; this.targetAssociation = null;
this.options = options; this.options = options;
this.sequelize = source.modelManager.sequelize; this.sequelize = source.modelManager.sequelize;
this.through = options.through; this.through = _.assign({}, options.through);
this.scope = options.scope; this.scope = options.scope;
this.isMultiAssociation = true; this.isMultiAssociation = true;
this.isSelfAssociation = this.source === this.target; this.isSelfAssociation = this.source === this.target;
...@@ -147,6 +147,7 @@ var BelongsToMany = function(source, target, options) { ...@@ -147,6 +147,7 @@ var BelongsToMany = function(source, target, options) {
if (this.options.through.model === association.options.through.model) { if (this.options.through.model === association.options.through.model) {
this.paired = association; this.paired = association;
association.paired = this;
} }
}, this); }, this);
......
...@@ -65,6 +65,51 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() { ...@@ -65,6 +65,51 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() {
}); });
}); });
describe('foreign keys', function() {
it('should infer otherKey from paired BTM relationship with a through string defined', function () {
var User = this.sequelize.define('User', {});
var Place = this.sequelize.define('Place', {});
var Places = User.belongsToMany(Place, { through: 'user_places', foreignKey: 'user_id' });
var Users = Place.belongsToMany(User, { through: 'user_places', foreignKey: 'place_id' });
expect(Places.paired).to.equal(Users);
expect(Users.paired).to.equal(Places);
expect(Places.foreignKey).to.equal('user_id');
expect(Users.foreignKey).to.equal('place_id');
expect(Places.otherKey).to.equal('place_id');
expect(Users.otherKey).to.equal('user_id');
});
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', {
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' });
expect(Places.paired).to.equal(Users);
expect(Users.paired).to.equal(Places);
expect(Places.foreignKey).to.equal('user_id');
expect(Users.foreignKey).to.equal('place_id');
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
});
});
describe('self-associations', function () { describe('self-associations', function () {
it('does not pair multiple self associations with different through arguments', function () { it('does not pair multiple self associations with different through arguments', function () {
var User = current.define('user', {}) var User = current.define('user', {})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!