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 2a1e5064
authored
Aug 23, 2017
by
pola88
Committed by
Sushant
Aug 23, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(belongs-to-many): support for polymorphic associations with scopes (#7533)
1 parent
6d19ea29
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
17 deletions
lib/associations/belongs-to-many.js
test/integration/associations/belongs-to-many.test.js
lib/associations/belongs-to-many.js
View file @
2a1e506
...
...
@@ -511,7 +511,7 @@ class BelongsToMany extends Association {
const
targetKey
=
association
.
target
.
primaryKeyAttribute
;
const
identifier
=
association
.
identifier
;
const
foreignIdentifier
=
association
.
foreignIdentifier
;
cons
t
where
=
{};
le
t
where
=
{};
if
(
newAssociatedObjects
===
null
)
{
newAssociatedObjects
=
[];
...
...
@@ -520,7 +520,7 @@ class BelongsToMany extends Association {
}
where
[
identifier
]
=
sourceInstance
.
get
(
sourceKey
);
_
.
assign
(
where
,
association
.
through
.
scope
);
where
=
Object
.
assign
(
where
,
association
.
through
.
scope
);
return
association
.
through
.
model
.
findAll
(
_
.
defaults
({
where
,
raw
:
true
},
options
)).
then
(
currentRows
=>
{
const
obsoleteAssociations
=
[];
...
...
@@ -559,10 +559,10 @@ class BelongsToMany extends Association {
}
if
(
obsoleteAssociations
.
length
>
0
)
{
cons
t
where
=
{};
le
t
where
=
{};
where
[
identifier
]
=
sourceInstance
.
get
(
sourceKey
);
where
[
foreignIdentifier
]
=
obsoleteAssociations
.
map
(
obsoleteAssociation
=>
obsoleteAssociation
[
foreignIdentifier
]);
where
=
Object
.
assign
(
where
,
association
.
through
.
scope
);
promises
.
push
(
association
.
through
.
model
.
destroy
(
_
.
defaults
({
where
},
options
)));
}
...
...
@@ -576,6 +576,7 @@ class BelongsToMany extends Association {
attributes
=
_
.
defaults
(
attributes
,
unassociatedObject
[
association
.
through
.
model
.
name
],
defaultAttributes
);
_
.
assign
(
attributes
,
association
.
through
.
scope
);
attributes
=
Object
.
assign
(
attributes
,
association
.
through
.
scope
);
return
attributes
;
});
...
...
test/integration/associations/belongs-to-many.test.js
View file @
2a1e506
...
...
@@ -617,6 +617,62 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
});
it
(
'using scope to set associations'
,
function
()
{
const
self
=
this
;
const
ItemTag
=
self
.
sequelize
.
define
(
'ItemTag'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
tag_id
:
{
type
:
DataTypes
.
INTEGER
,
unique
:
false
},
taggable
:
{
type
:
DataTypes
.
STRING
},
taggable_id
:
{
type
:
DataTypes
.
INTEGER
,
unique
:
false
}
}),
Tag
=
self
.
sequelize
.
define
(
'Tag'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
name
:
DataTypes
.
STRING
}),
Comment
=
self
.
sequelize
.
define
(
'Comment'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
name
:
DataTypes
.
STRING
}),
Post
=
self
.
sequelize
.
define
(
'Post'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
name
:
DataTypes
.
STRING
});
Post
.
belongsToMany
(
Tag
,
{
through
:
{
model
:
ItemTag
,
unique
:
false
,
scope
:
{
taggable
:
'post'
}
},
foreignKey
:
'taggable_id'
});
Comment
.
belongsToMany
(
Tag
,
{
through
:
{
model
:
ItemTag
,
unique
:
false
,
scope
:
{
taggable
:
'comment'
}
},
foreignKey
:
'taggable_id'
});
return
self
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
all
([
Post
.
create
({
name
:
'post1'
}),
Comment
.
create
({
name
:
'comment1'
}),
Tag
.
create
({
name
:
'tag1'
})
]);
}).
bind
({}).
spread
(
(
post
,
comment
,
tag
)
=>
{
self
.
post
=
post
;
self
.
comment
=
comment
;
self
.
tag
=
tag
;
return
self
.
post
.
setTags
([
self
.
tag
]);
}).
then
(
()
=>
{
return
self
.
comment
.
setTags
([
self
.
tag
]);
}).
then
(
()
=>
{
return
Promise
.
all
([
self
.
post
.
getTags
(),
self
.
comment
.
getTags
()
]);
}).
spread
(
(
postTags
,
commentTags
)
=>
{
expect
(
postTags
).
to
.
have
.
length
(
1
);
expect
(
commentTags
).
to
.
have
.
length
(
1
);
});
});
it
(
'updating association via set associations with scope'
,
function
()
{
const
self
=
this
;
const
ItemTag
=
this
.
sequelize
.
define
(
'ItemTag'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
tag_id
:
{
type
:
DataTypes
.
INTEGER
,
unique
:
false
},
...
...
@@ -646,23 +702,31 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
foreignKey
:
'taggable_id'
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
()
=>
{
return
Promise
.
all
([
Post
.
create
({
name
:
'post1'
}),
Comment
.
create
({
name
:
'comment1'
}),
Tag
.
create
({
name
:
'tag1'
})
Tag
.
create
({
name
:
'tag1'
}),
Tag
.
create
({
name
:
'tag2'
})
]);
}).
bind
({}).
spread
(
function
(
post
,
comment
,
tag
)
{
this
.
post
=
post
;
this
.
comment
=
comment
;
this
.
tag
=
tag
;
return
this
.
post
.
setTags
([
this
.
tag
]);
}).
then
(
function
()
{
return
this
.
comment
.
setTags
([
this
.
tag
]);
}).
then
(
function
()
{
return
this
.
comment
.
getTags
();
}).
then
(
_tags
=>
{
expect
(
_tags
).
to
.
have
.
length
(
1
);
}).
bind
({}).
spread
(
(
post
,
comment
,
tag
,
secondTag
)
=>
{
self
.
post
=
post
;
self
.
comment
=
comment
;
self
.
tag
=
tag
;
self
.
secondTag
=
secondTag
;
return
self
.
post
.
setTags
([
self
.
tag
,
self
.
secondTag
]);
}).
then
(
()
=>
{
return
self
.
comment
.
setTags
([
self
.
tag
,
self
.
secondTag
]);
}).
then
(
()
=>
{
return
self
.
post
.
setTags
([
self
.
tag
]);
}).
then
(
()
=>
{
return
Promise
.
all
([
self
.
post
.
getTags
(),
self
.
comment
.
getTags
()
]);
}).
spread
(
(
postTags
,
commentTags
)
=>
{
expect
(
postTags
).
to
.
have
.
length
(
1
);
expect
(
commentTags
).
to
.
have
.
length
(
2
);
});
});
});
...
...
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