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 da57c5b5
authored
Aug 20, 2015
by
Ryan Fink
Committed by
Jan Aagaard Meier
Oct 08, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix for onDuplicateKeyUpdate in mysql
1 parent
930e6537
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
2 deletions
lib/dialects/mysql/query-generator.js
test/unit/sql/insert.js
lib/dialects/mysql/query-generator.js
View file @
da57c5b
...
...
@@ -167,7 +167,7 @@ var QueryGenerator = {
return
this
.
insertQuery
(
tableName
,
insertValues
,
rawAttributes
,
options
);
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
)
{
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
,
rawAttributes
)
{
var
query
=
'INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %><%= onDuplicateKeyUpdate %>;'
,
tuples
=
[]
,
allAttributes
=
[]
...
...
@@ -189,7 +189,8 @@ var QueryGenerator = {
if
(
options
&&
options
.
updateOnDuplicate
)
{
onDuplicateKeyUpdate
+=
' ON DUPLICATE KEY UPDATE '
+
options
.
updateOnDuplicate
.
map
(
function
(
attr
)
{
var
key
=
this
.
quoteIdentifier
(
attr
);
var
field
=
rawAttributes
&&
rawAttributes
[
attr
]
&&
rawAttributes
[
attr
].
field
||
attr
;
var
key
=
this
.
quoteIdentifier
(
field
);
return
key
+
'=VALUES('
+
key
+
')'
;
}.
bind
(
this
)).
join
(
','
);
}
...
...
test/unit/sql/insert.js
View file @
da57c5b
...
...
@@ -35,4 +35,41 @@ describe(Support.getTestDialectTeaser('SQL'), function() {
});
});
describe
(
'bulkCreate'
,
function
()
{
it
(
'bulk create with onDuplicateKeyUpdate'
,
function
()
{
// Skip mssql for now, it seems broken
if
(
Support
.
getTestDialect
()
===
'mssql'
)
{
return
;
}
var
User
=
Support
.
sequelize
.
define
(
'user'
,
{
username
:
{
type
:
DataTypes
.
STRING
,
field
:
'user_name'
},
password
:
{
type
:
DataTypes
.
STRING
,
field
:
'pass_word'
},
createdAt
:
{
type
:
DataTypes
.
DATE
,
field
:
'created_at'
},
updatedAt
:
{
type
:
DataTypes
.
DATE
,
field
:
'updated_at'
}
},{
timestamps
:
true
});
expectsql
(
sql
.
bulkInsertQuery
(
User
.
tableName
,
[{
user_name
:
'testuser'
,
pass_word
:
'12345'
}],
{
updateOnDuplicate
:
[
'username'
,
'password'
,
'updatedAt'
]
},
User
.
rawAttributes
),
{
default
:
'INSERT INTO `users` (`user_name`,`pass_word`) VALUES (\'testuser\',\'12345\');'
,
postgres
:
'INSERT INTO "users" ("user_name","pass_word") VALUES (\'testuser\',\'12345\');'
,
mysql
:
'INSERT INTO `users` (`user_name`,`pass_word`) VALUES (\'testuser\',\'12345\') ON DUPLICATE KEY UPDATE `user_name`=VALUES(`user_name`),`pass_word`=VALUES(`pass_word`),`updated_at`=VALUES(`updated_at`);'
});
});
});
});
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