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

Commit 0b5adc65 by Mick Hansen

fix(belongs-to-many): correctly throw error when no 'as' is defined for self N:M

1 parent 77fd8baf
...@@ -61,6 +61,10 @@ var BelongsToMany = function(source, target, options) { ...@@ -61,6 +61,10 @@ var BelongsToMany = function(source, target, options) {
this.doubleLinked = false; this.doubleLinked = false;
this.as = this.options.as; this.as = this.options.as;
if (!this.as && this.isSelfAssociation) {
throw new Error('\'as\' must be defined for many-to-many self-associations');
}
if (this.as) { if (this.as) {
this.isAliased = true; this.isAliased = true;
...@@ -87,10 +91,6 @@ var BelongsToMany = function(source, target, options) { ...@@ -87,10 +91,6 @@ var BelongsToMany = function(source, target, options) {
* If self association, this is the target association - Unless we find a pairing association * If self association, this is the target association - Unless we find a pairing association
*/ */
if (this.isSelfAssociation) { if (this.isSelfAssociation) {
if (!this.as) {
throw new Error('\'as\' must be defined for many-to-many self-associations');
}
this.targetAssociation = this; this.targetAssociation = this;
} }
......
...@@ -117,10 +117,12 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() { ...@@ -117,10 +117,12 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() {
, Invite = current.define('invite', {}); , Invite = current.define('invite', {});
User.Followers = User.belongsToMany(User, { User.Followers = User.belongsToMany(User, {
through: UserFollowers as: 'Followers',
through: UserFollowers,
}); });
User.Invites = User.belongsToMany(User, { User.Invites = User.belongsToMany(User, {
as: 'Invites',
foreignKey: 'InviteeId', foreignKey: 'InviteeId',
through: Invite through: Invite
}); });
...@@ -131,6 +133,29 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() { ...@@ -131,6 +133,29 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() {
expect(User.Followers.otherKey).not.to.equal(User.Invites.foreignKey); expect(User.Followers.otherKey).not.to.equal(User.Invites.foreignKey);
}); });
it('correctly generates a foreign/other key when none are defined', function () {
var User = current.define('user', {})
, UserFollowers = current.define('userFollowers', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
}
}, {
timestamps: false
});
User.Followers = User.belongsToMany(User, {
as: 'Followers',
through: UserFollowers
});
expect(User.Followers.foreignKey).to.be.ok;
expect(User.Followers.otherKey).to.be.ok;
expect(Object.keys(UserFollowers.rawAttributes).length).to.equal(3);
});
it('works with singular and plural name for self-associations', function () { it('works with singular and plural name for self-associations', function () {
// Models taken from https://github.com/sequelize/sequelize/issues/3796 // Models taken from https://github.com/sequelize/sequelize/issues/3796
var Service = current.define('service', {}) var Service = current.define('service', {})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!