Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 0b5adc65
authored
Jul 20, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(belongs-to-many): correctly throw error when no 'as' is defined for self N:M
1 parent
77fd8baf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
5 deletions
lib/associations/belongs-to-many.js
test/unit/associations/belongs-to-many.test.js
lib/associations/belongs-to-many.js
View file @
0b5adc6
...
@@ -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
;
}
}
...
...
test/unit/associations/belongs-to-many.test.js
View file @
0b5adc6
...
@@ -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'
,
{})
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment