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 a0635e3c
authored
Mar 16, 2020
by
Evan
Committed by
GitHub
Mar 16, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(model): updateOnDuplicate handles composite keys (#11984)
1 parent
f98c59c1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
112 additions
and
1 deletions
lib/model.js
test/integration/model/bulk-create.test.js
lib/model.js
View file @
a0635e3
...
@@ -2708,7 +2708,7 @@ class Model {
...
@@ -2708,7 +2708,7 @@ class Model {
// Get primary keys for postgres to enable updateOnDuplicate
// Get primary keys for postgres to enable updateOnDuplicate
options
.
upsertKeys
=
_
.
chain
(
model
.
primaryKeys
).
values
().
map
(
'field'
).
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
(
c
=>
c
.
fields
[
0
]).
value
();
options
.
upsertKeys
=
_
.
chain
(
model
.
uniqueKeys
).
values
().
filter
(
c
=>
c
.
fields
.
length
>=
1
).
map
(
c
=>
c
.
fields
).
reduce
(
c
=>
c
[
0
]).
value
();
}
}
}
}
...
...
test/integration/model/bulk-create.test.js
View file @
a0635e3
...
@@ -586,6 +586,117 @@ describe(Support.getTestDialectTeaser('Model'), () => {
...
@@ -586,6 +586,117 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect
(
people
[
1
].
name
).
to
.
equal
(
'Bob'
);
expect
(
people
[
1
].
name
).
to
.
equal
(
'Bob'
);
});
});
});
});
it
(
'when the composite primary key column names and model field names are different'
,
function
()
{
const
Person
=
this
.
sequelize
.
define
(
'Person'
,
{
systemId
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
field
:
'system_id'
},
system
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
,
primaryKey
:
true
,
field
:
'system'
},
name
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
,
field
:
'name'
}
},
{});
return
Person
.
sync
({
force
:
true
})
.
then
(()
=>
{
const
inserts
=
[
{
systemId
:
1
,
system
:
'system1'
,
name
:
'Alice'
}
];
return
Person
.
bulkCreate
(
inserts
);
})
.
then
(
people
=>
{
expect
(
people
.
length
).
to
.
equal
(
1
);
expect
(
people
[
0
].
systemId
).
to
.
equal
(
1
);
expect
(
people
[
0
].
system
).
to
.
equal
(
'system1'
);
expect
(
people
[
0
].
name
).
to
.
equal
(
'Alice'
);
const
updates
=
[
{
systemId
:
1
,
system
:
'system1'
,
name
:
'CHANGED NAME'
},
{
systemId
:
1
,
system
:
'system2'
,
name
:
'Bob'
}
];
return
Person
.
bulkCreate
(
updates
,
{
updateOnDuplicate
:
[
'systemId'
,
'system'
,
'name'
]
});
})
.
then
(
people
=>
{
expect
(
people
.
length
).
to
.
equal
(
2
);
expect
(
people
[
0
].
systemId
).
to
.
equal
(
1
);
expect
(
people
[
0
].
system
).
to
.
equal
(
'system1'
);
expect
(
people
[
0
].
name
).
to
.
equal
(
'CHANGED NAME'
);
expect
(
people
[
1
].
systemId
).
to
.
equal
(
1
);
expect
(
people
[
1
].
system
).
to
.
equal
(
'system2'
);
expect
(
people
[
1
].
name
).
to
.
equal
(
'Bob'
);
});
});
it
(
'when the primary key column names and model field names are different and have composite unique constraints'
,
function
()
{
const
Person
=
this
.
sequelize
.
define
(
'Person'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
field
:
'id'
},
systemId
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
unique
:
'system_id_system_unique'
,
field
:
'system_id'
},
system
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
,
unique
:
'system_id_system_unique'
,
field
:
'system'
},
name
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
,
field
:
'name'
}
},
{});
return
Person
.
sync
({
force
:
true
})
.
then
(()
=>
{
const
inserts
=
[
{
id
:
1
,
systemId
:
1
,
system
:
'system1'
,
name
:
'Alice'
}
];
return
Person
.
bulkCreate
(
inserts
);
})
.
then
(
people
=>
{
expect
(
people
.
length
).
to
.
equal
(
1
);
expect
(
people
[
0
].
systemId
).
to
.
equal
(
1
);
expect
(
people
[
0
].
system
).
to
.
equal
(
'system1'
);
expect
(
people
[
0
].
name
).
to
.
equal
(
'Alice'
);
const
updates
=
[
{
id
:
1
,
systemId
:
1
,
system
:
'system1'
,
name
:
'CHANGED NAME'
},
{
id
:
2
,
systemId
:
1
,
system
:
'system2'
,
name
:
'Bob'
}
];
return
Person
.
bulkCreate
(
updates
,
{
updateOnDuplicate
:
[
'systemId'
,
'system'
,
'name'
]
});
})
.
then
(
people
=>
{
expect
(
people
.
length
).
to
.
equal
(
2
);
expect
(
people
[
0
].
systemId
).
to
.
equal
(
1
);
expect
(
people
[
0
].
system
).
to
.
equal
(
'system1'
);
expect
(
people
[
0
].
name
).
to
.
equal
(
'CHANGED NAME'
);
expect
(
people
[
1
].
systemId
).
to
.
equal
(
1
);
expect
(
people
[
1
].
system
).
to
.
equal
(
'system2'
);
expect
(
people
[
1
].
name
).
to
.
equal
(
'Bob'
);
});
});
});
});
...
...
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