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 7b80ade6
authored
Apr 02, 2016
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support calling setAssociation twice on hasOne. Closes #5315
1 parent
8ab2dd32
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
3 deletions
changelog.md
lib/associations/has-one.js
test/integration/associations/belongs-to.test.js
test/integration/associations/has-one.test.js
changelog.md
View file @
7b80ade
# Future
# Future
-
[
FIXED
]
Confirmed that values modified in validation hooks are preserved
[
#3534
](
https://github.com/sequelize/sequelize/issues/3534
)
-
[
FIXED
]
Confirmed that values modified in validation hooks are preserved
[
#3534
](
https://github.com/sequelize/sequelize/issues/3534
)
-
[
FIXED
]
Support lower case type names in SQLite
[
#5482
](
https://github.com/sequelize/sequelize/issues/5482
)
-
[
FIXED
]
Support lower case type names in SQLite
[
#5482
](
https://github.com/sequelize/sequelize/issues/5482
)
-
[
FIXED
]
Support calling
`setAssociation`
twice on
`hasOne`
[
#5315
](
https://github.com/sequelize/sequelize/issues/5315
)
-
[
INTERNALS
]
Removed dependency on wellknown in favor of terraformer-wkt-parser
-
[
INTERNALS
]
Removed dependency on wellknown in favor of terraformer-wkt-parser
-
[
ADDED
]
Benchmarking feature
[
#2494
](
https://github.com/sequelize/sequelize/issues/2494
)
-
[
ADDED
]
Benchmarking feature
[
#2494
](
https://github.com/sequelize/sequelize/issues/2494
)
...
...
lib/associations/has-one.js
View file @
7b80ade
...
@@ -173,12 +173,18 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
...
@@ -173,12 +173,18 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
var
association
=
this
;
var
association
=
this
;
instancePrototype
[
this
.
accessors
.
set
]
=
function
(
associatedInstance
,
options
)
{
instancePrototype
[
this
.
accessors
.
set
]
=
function
(
associatedInstance
,
options
)
{
var
instance
=
this
;
var
instance
=
this
,
alreadyAssociated
;
options
=
options
||
{};
options
=
options
||
{};
options
.
scope
=
false
;
options
.
scope
=
false
;
return
instance
[
association
.
accessors
.
get
](
options
).
then
(
function
(
oldInstance
)
{
return
instance
[
association
.
accessors
.
get
](
options
).
then
(
function
(
oldInstance
)
{
if
(
oldInstance
)
{
// TODO Use equals method once #5605 is resolved
alreadyAssociated
=
oldInstance
&&
associatedInstance
&&
_
.
every
(
association
.
target
.
primaryKeyAttributes
,
function
(
attribute
)
{
return
oldInstance
.
get
(
attribute
,
{
raw
:
true
})
===
associatedInstance
.
get
(
attribute
,
{
raw
:
true
});
});
if
(
oldInstance
&&
!
alreadyAssociated
)
{
oldInstance
[
association
.
foreignKey
]
=
null
;
oldInstance
[
association
.
foreignKey
]
=
null
;
return
oldInstance
.
save
(
_
.
extend
({},
options
,
{
return
oldInstance
.
save
(
_
.
extend
({},
options
,
{
fields
:
[
association
.
foreignKey
],
fields
:
[
association
.
foreignKey
],
...
@@ -187,7 +193,7 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
...
@@ -187,7 +193,7 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
}));
}));
}
}
}).
then
(
function
()
{
}).
then
(
function
()
{
if
(
associatedInstance
)
{
if
(
associatedInstance
&&
!
alreadyAssociated
)
{
if
(
!
(
associatedInstance
instanceof
association
.
target
.
Instance
))
{
if
(
!
(
associatedInstance
instanceof
association
.
target
.
Instance
))
{
var
tmpInstance
=
{};
var
tmpInstance
=
{};
tmpInstance
[
association
.
target
.
primaryKeyAttribute
]
=
associatedInstance
;
tmpInstance
[
association
.
target
.
primaryKeyAttribute
]
=
associatedInstance
;
...
...
test/integration/associations/belongs-to.test.js
View file @
7b80ade
...
@@ -334,6 +334,28 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
...
@@ -334,6 +334,28 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
});
});
});
});
});
});
it
(
'supports setting same association twice'
,
function
()
{
var
Home
=
this
.
sequelize
.
define
(
'home'
,
{})
,
User
=
this
.
sequelize
.
define
(
'user'
);
Home
.
belongsTo
(
User
);
return
this
.
sequelize
.
sync
({
force
:
true
}).
bind
({}).
then
(
function
()
{
return
Promise
.
all
([
Home
.
create
(),
User
.
create
()
]);
}).
spread
(
function
(
home
,
user
)
{
this
.
home
=
home
;
this
.
user
=
user
;
return
home
.
setUser
(
user
);
}).
then
(
function
()
{
return
this
.
home
.
setUser
(
this
.
user
);
}).
then
(
function
()
{
return
expect
(
this
.
home
.
getUser
()).
to
.
eventually
.
have
.
property
(
'id'
,
this
.
user
.
get
(
'id'
));
});
});
});
});
describe
(
'createAssociation'
,
function
()
{
describe
(
'createAssociation'
,
function
()
{
...
...
test/integration/associations/has-one.test.js
View file @
7b80ade
...
@@ -217,6 +217,28 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
...
@@ -217,6 +217,28 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
});
});
});
});
});
});
it
(
'supports setting same association twice'
,
function
()
{
var
Home
=
this
.
sequelize
.
define
(
'home'
,
{})
,
User
=
this
.
sequelize
.
define
(
'user'
);
User
.
hasOne
(
Home
);
return
this
.
sequelize
.
sync
({
force
:
true
}).
bind
({}).
then
(
function
()
{
return
Promise
.
all
([
Home
.
create
(),
User
.
create
()
]);
}).
spread
(
function
(
home
,
user
)
{
this
.
home
=
home
;
this
.
user
=
user
;
return
user
.
setHome
(
home
);
}).
then
(
function
()
{
return
this
.
user
.
setHome
(
this
.
home
);
}).
then
(
function
()
{
return
expect
(
this
.
user
.
getHome
()).
to
.
eventually
.
have
.
property
(
'id'
,
this
.
home
.
get
(
'id'
));
});
});
});
});
describe
(
'createAssociation'
,
function
()
{
describe
(
'createAssociation'
,
function
()
{
...
...
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