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 3a600693
authored
Sep 19, 2019
by
PeledYuval
Committed by
Sushant
Sep 19, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(bulkCreate): use correct primary key column name for updateOnDuplicate (#11434)
1 parent
5047bda1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
2 deletions
lib/model.js
test/integration/model/bulk-create.test.js
lib/model.js
View file @
3a60069
...
@@ -2700,7 +2700,7 @@ class Model {
...
@@ -2700,7 +2700,7 @@ class Model {
if
(
options
.
updateOnDuplicate
)
{
if
(
options
.
updateOnDuplicate
)
{
options
.
updateOnDuplicate
=
options
.
updateOnDuplicate
.
map
(
attr
=>
model
.
rawAttributes
[
attr
].
field
||
attr
);
options
.
updateOnDuplicate
=
options
.
updateOnDuplicate
.
map
(
attr
=>
model
.
rawAttributes
[
attr
].
field
||
attr
);
// Get primary keys for postgres to enable updateOnDuplicate
// Get primary keys for postgres to enable updateOnDuplicate
options
.
upsertKeys
=
_
.
chain
(
model
.
primaryKeys
).
values
().
map
(
'field
Name
'
).
value
();
options
.
upsertKeys
=
_
.
chain
(
model
.
primaryKeys
).
values
().
map
(
'field'
).
value
();
if
(
Object
.
keys
(
model
.
uniqueKeys
).
length
>
0
)
{
if
(
Object
.
keys
(
model
.
uniqueKeys
).
length
>
0
)
{
options
.
upsertKeys
=
_
.
chain
(
model
.
uniqueKeys
).
values
().
filter
(
c
=>
c
.
fields
.
length
===
1
).
map
(
'column'
).
value
();
options
.
upsertKeys
=
_
.
chain
(
model
.
uniqueKeys
).
values
().
filter
(
c
=>
c
.
fields
.
length
===
1
).
map
(
'column'
).
value
();
}
}
...
...
test/integration/model/bulk-create.test.js
View file @
3a60069
...
@@ -34,6 +34,16 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -34,6 +34,16 @@ describe(Support.getTestDialectTeaser('Model'), () => {
no
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
},
no
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
},
name
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
}
name
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
}
});
});
this
.
Car
=
this
.
sequelize
.
define
(
'Car'
,
{
plateNumber
:
{
type
:
DataTypes
.
STRING
,
primaryKey
:
true
,
field
:
'plate_number'
},
color
:
{
type
:
DataTypes
.
TEXT
}
});
return
this
.
sequelize
.
sync
({
force
:
true
});
return
this
.
sequelize
.
sync
({
force
:
true
});
});
});
...
@@ -480,7 +490,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -480,7 +490,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
});
it
(
'should support the updateOnDuplicate option with primary keys'
,
function
()
{
describe
(
'should support the updateOnDuplicate option with primary keys'
,
()
=>
{
it
(
'when the primary key column names and model field names are the same'
,
function
()
{
const
data
=
[
const
data
=
[
{
no
:
1
,
name
:
'Peter'
},
{
no
:
1
,
name
:
'Peter'
},
{
no
:
2
,
name
:
'Paul'
}
{
no
:
2
,
name
:
'Paul'
}
...
@@ -506,6 +517,33 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -506,6 +517,33 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
});
it
(
'when the primary key column names and model field names are different'
,
function
()
{
const
data
=
[
{
plateNumber
:
'abc'
,
color
:
'Grey'
},
{
plateNumber
:
'def'
,
color
:
'White'
}
];
return
this
.
Car
.
bulkCreate
(
data
,
{
fields
:
[
'plateNumber'
,
'color'
],
updateOnDuplicate
:
[
'color'
]
}).
then
(()
=>
{
const
new_data
=
[
{
plateNumber
:
'abc'
,
color
:
'Red'
},
{
plateNumber
:
'def'
,
color
:
'Green'
},
{
plateNumber
:
'ghi'
,
color
:
'Blue'
}
];
return
this
.
Car
.
bulkCreate
(
new_data
,
{
fields
:
[
'plateNumber'
,
'color'
],
updateOnDuplicate
:
[
'color'
]
}).
then
(()
=>
{
return
this
.
Car
.
findAll
({
order
:
[
'plateNumber'
]
}).
then
(
cars
=>
{
expect
(
cars
.
length
).
to
.
equal
(
3
);
expect
(
cars
[
0
].
plateNumber
).
to
.
equal
(
'abc'
);
expect
(
cars
[
0
].
color
).
to
.
equal
(
'Red'
);
expect
(
cars
[
1
].
plateNumber
).
to
.
equal
(
'def'
);
expect
(
cars
[
1
].
color
).
to
.
equal
(
'Green'
);
expect
(
cars
[
2
].
plateNumber
).
to
.
equal
(
'ghi'
);
expect
(
cars
[
2
].
color
).
to
.
equal
(
'Blue'
);
});
});
});
});
});
it
(
'should reject for non array updateOnDuplicate option'
,
function
()
{
it
(
'should reject for non array updateOnDuplicate option'
,
function
()
{
const
data
=
[
const
data
=
[
{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
...
...
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