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 fdf3e99e
authored
Sep 07, 2017
by
Lars Tijhuis
Committed by
Sushant
Sep 07, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(bulkCreate): error with updateOnDuplicate option (#8259)
1 parent
8a95e421
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
7 deletions
lib/model.js
test/integration/model/create.test.js
lib/model.js
View file @
fdf3e99
...
...
@@ -2357,7 +2357,7 @@ class Model {
return
this
.
QueryInterface
.
bulkInsert
(
this
.
getTableName
(
options
),
records
,
options
,
attributes
).
then
(
results
=>
{
if
(
Array
.
isArray
(
results
))
{
results
.
forEach
((
result
,
i
)
=>
{
instances
[
i
]
.
set
(
this
.
primaryKeyAttribute
,
result
[
this
.
rawAttributes
[
this
.
primaryKeyAttribute
].
f
ield
],
{
raw
:
true
});
instances
[
i
]
&&
instances
[
i
].
set
(
this
.
primaryKeyAttribute
,
result
[
this
.
primaryKeyF
ield
],
{
raw
:
true
});
});
}
return
results
;
...
...
test/integration/model/create.test.js
View file @
fdf3e99
...
...
@@ -1760,12 +1760,15 @@ describe(Support.getTestDialectTeaser('Model'), () => {
if
(
dialect
!==
'postgres'
&&
dialect
!==
'mssql'
)
{
it
(
'should support the ignoreDuplicates option'
,
function
()
{
const
self
=
this
,
data
=
[{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'23'
}];
const
self
=
this
;
const
data
=
[
{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'23'
}
];
return
this
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
]
}).
then
(()
=>
{
data
.
push
({
uniqueName
:
'Michael'
,
secretValue
:
'26'
});
return
self
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
],
ignoreDuplicates
:
true
}).
then
(()
=>
{
return
self
.
User
.
findAll
({
order
:
[
'id'
]}).
then
(
users
=>
{
expect
(
users
.
length
).
to
.
equal
(
3
);
...
...
@@ -1781,9 +1784,11 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
}
else
{
it
(
'should throw an error when the ignoreDuplicates option is passed'
,
function
()
{
const
self
=
this
,
data
=
[{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'23'
}];
const
self
=
this
;
const
data
=
[
{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'23'
}
];
return
this
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
]
}).
then
(()
=>
{
data
.
push
({
uniqueName
:
'Michael'
,
secretValue
:
'26'
});
...
...
@@ -1799,6 +1804,35 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
}
if
(
current
.
dialect
.
supports
.
updateOnDuplicate
)
{
it
(
'should support the updateOnDuplicate option'
,
function
()
{
const
self
=
this
;
const
data
=
[
{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'23'
}
];
return
this
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
],
updateOnDuplicate
:
[
'secretValue'
]
}).
then
(()
=>
{
const
new_data
=
[
{
uniqueName
:
'Peter'
,
secretValue
:
'43'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'24'
},
{
uniqueName
:
'Michael'
,
secretValue
:
'26'
}
];
return
self
.
User
.
bulkCreate
(
new_data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
],
updateOnDuplicate
:
[
'secretValue'
]
}).
then
(()
=>
{
return
self
.
User
.
findAll
({
order
:
[
'id'
]}).
then
(
users
=>
{
expect
(
users
.
length
).
to
.
equal
(
3
);
expect
(
users
[
0
].
uniqueName
).
to
.
equal
(
'Peter'
);
expect
(
users
[
0
].
secretValue
).
to
.
equal
(
'43'
);
expect
(
users
[
1
].
uniqueName
).
to
.
equal
(
'Paul'
);
expect
(
users
[
1
].
secretValue
).
to
.
equal
(
'24'
);
expect
(
users
[
2
].
uniqueName
).
to
.
equal
(
'Michael'
);
expect
(
users
[
2
].
secretValue
).
to
.
equal
(
'26'
);
});
});
});
});
}
if
(
current
.
dialect
.
supports
.
returnValues
)
{
describe
(
'return values'
,
()
=>
{
it
(
'should make the autoincremented values available on the returned instances'
,
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