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 7a49a658
authored
Jun 24, 2018
by
jony89
Committed by
Sushant
Jun 24, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(model): ignore undefined values in update payload (#9587)
1 parent
bb12f8d5
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
2 deletions
lib/model.js
test/integration/instance/update.test.js
test/integration/model/update.test.js
lib/model.js
View file @
7a49a65
...
@@ -2847,8 +2847,8 @@ class Model {
...
@@ -2847,8 +2847,8 @@ class Model {
options
.
type
=
QueryTypes
.
BULKUPDATE
;
options
.
type
=
QueryTypes
.
BULKUPDATE
;
// Clone values so it doesn't get modified for caller scope
// Clone values so it doesn't get modified for caller scope
and ignore undefined values
values
=
_
.
clone
(
values
);
values
=
_
.
omitBy
(
values
,
value
=>
value
===
undefined
);
// Remove values that are not in the options.fields
// Remove values that are not in the options.fields
if
(
options
.
fields
&&
options
.
fields
instanceof
Array
)
{
if
(
options
.
fields
&&
options
.
fields
instanceof
Array
)
{
...
@@ -3913,6 +3913,9 @@ class Model {
...
@@ -3913,6 +3913,9 @@ class Model {
* @return {Promise<this>}
* @return {Promise<this>}
*/
*/
update
(
values
,
options
)
{
update
(
values
,
options
)
{
// Clone values so it doesn't get modified for caller scope and ignore undefined values
values
=
_
.
omitBy
(
values
,
value
=>
value
===
undefined
);
const
changedBefore
=
this
.
changed
()
||
[];
const
changedBefore
=
this
.
changed
()
||
[];
options
=
options
||
{};
options
=
options
||
{};
...
...
test/integration/instance/update.test.js
View file @
7a49a65
...
@@ -374,6 +374,16 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
...
@@ -374,6 +374,16 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
});
});
});
});
it
(
'ignores undefined attributes'
,
function
()
{
return
this
.
User
.
sync
({
force
:
true
}).
bind
(
this
).
then
(()
=>
{
return
this
.
User
.
create
({
username
:
'user'
}).
then
(
user
=>
{
return
user
.
update
({
username
:
undefined
}).
then
(
user
=>
{
expect
(
user
.
username
).
to
.
equal
(
'user'
);
});
});
});
});
it
(
'doesn\'t update primary keys or timestamps'
,
function
()
{
it
(
'doesn\'t update primary keys or timestamps'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
const
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
name
:
DataTypes
.
STRING
,
name
:
DataTypes
.
STRING
,
...
...
test/integration/model/update.test.js
View file @
7a49a65
...
@@ -35,6 +35,26 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -35,6 +35,26 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}));
}));
});
});
it
(
'should ignore undefined values without throwing not null validation'
,
function
()
{
const
ownerId
=
2
;
return
this
.
Account
.
create
({
ownerId
,
name
:
Math
.
random
().
toString
()
}).
then
(
account
=>
{
return
this
.
Account
.
update
({
name
:
Math
.
random
().
toString
(),
ownerId
:
undefined
},
{
where
:
{
id
:
account
.
get
(
'id'
)
}
});
}).
then
(()
=>
{
return
this
.
Account
.
findOne
();
}).
then
(
account
=>
{
expect
(
account
.
ownerId
).
to
.
be
.
equal
(
ownerId
);
});
});
if
(
_
.
get
(
current
.
dialect
.
supports
,
'returnValues.returning'
))
{
if
(
_
.
get
(
current
.
dialect
.
supports
,
'returnValues.returning'
))
{
it
(
'should return the updated record'
,
function
()
{
it
(
'should return the updated record'
,
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