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 11a92a2b
authored
Jan 30, 2016
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5298 from PhinCo/master
Fix for bug #4755
2 parents
68e61305
0e4e7045
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
10 deletions
lib/query-interface.js
test/integration/model/upsert.test.js
lib/query-interface.js
View file @
11a92a2
...
@@ -495,20 +495,23 @@ QueryInterface.prototype.insert = function(instance, tableName, values, options)
...
@@ -495,20 +495,23 @@ QueryInterface.prototype.insert = function(instance, tableName, values, options)
});
});
};
};
QueryInterface
.
prototype
.
upsert
=
function
(
tableName
,
values
,
updateValues
,
model
,
options
)
{
QueryInterface
.
prototype
.
upsert
=
function
(
tableName
,
values
ByField
,
updateValues
,
model
,
options
)
{
var
wheres
=
[]
var
wheres
=
[]
,
where
,
where
,
indexFields
,
indexFields
,
indexes
=
[]
,
indexes
=
[]
,
attributes
=
Object
.
keys
(
values
);
,
attributes
=
Object
.
keys
(
values
ByField
);
where
=
{};
where
=
model
.
primaryKeyAttributes
.
reduce
(
function
(
where
,
key
)
{
for
(
var
i
=
0
;
i
<
model
.
primaryKeyAttributes
.
length
;
i
++
)
{
var
attribute
=
model
.
rawAttributes
[
key
];
var
key
=
model
.
primaryKeyAttributes
[
i
];
if
(
key
in
values
){
if
(
attribute
.
field
&&
attribute
.
field
in
valuesByField
)
{
where
[
key
]
=
values
[
key
];
where
[
attribute
.
field
]
=
valuesByField
[
attribute
.
field
];
}
else
if
(
key
in
valuesByField
)
{
where
[
key
]
=
valuesByField
[
key
];
}
}
}
return
where
;
},
{}
);
if
(
!
Utils
.
_
.
isEmpty
(
where
))
{
if
(
!
Utils
.
_
.
isEmpty
(
where
))
{
wheres
.
push
(
where
);
wheres
.
push
(
where
);
...
@@ -536,7 +539,7 @@ QueryInterface.prototype.upsert = function(tableName, values, updateValues, mode
...
@@ -536,7 +539,7 @@ QueryInterface.prototype.upsert = function(tableName, values, updateValues, mode
if
(
Utils
.
_
.
intersection
(
attributes
,
index
).
length
===
index
.
length
)
{
if
(
Utils
.
_
.
intersection
(
attributes
,
index
).
length
===
index
.
length
)
{
where
=
{};
where
=
{};
index
.
forEach
(
function
(
field
)
{
index
.
forEach
(
function
(
field
)
{
where
[
field
]
=
values
[
field
];
where
[
field
]
=
values
ByField
[
field
];
});
});
wheres
.
push
(
where
);
wheres
.
push
(
where
);
}
}
...
@@ -547,7 +550,7 @@ QueryInterface.prototype.upsert = function(tableName, values, updateValues, mode
...
@@ -547,7 +550,7 @@ QueryInterface.prototype.upsert = function(tableName, values, updateValues, mode
options
.
type
=
QueryTypes
.
UPSERT
;
options
.
type
=
QueryTypes
.
UPSERT
;
options
.
raw
=
true
;
options
.
raw
=
true
;
var
sql
=
this
.
QueryGenerator
.
upsertQuery
(
tableName
,
values
,
updateValues
,
where
,
model
.
rawAttributes
,
options
);
var
sql
=
this
.
QueryGenerator
.
upsertQuery
(
tableName
,
values
ByField
,
updateValues
,
where
,
model
.
rawAttributes
,
options
);
return
this
.
sequelize
.
query
(
sql
,
options
).
then
(
function
(
rowCount
)
{
return
this
.
sequelize
.
query
(
sql
,
options
).
then
(
function
(
rowCount
)
{
if
(
rowCount
===
undefined
)
{
if
(
rowCount
===
undefined
)
{
return
rowCount
;
return
rowCount
;
...
...
test/integration/model/upsert.test.js
View file @
11a92a2
...
@@ -31,6 +31,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -31,6 +31,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
blob
:
DataTypes
.
BLOB
blob
:
DataTypes
.
BLOB
});
});
this
.
ModelWithFieldPK
=
this
.
sequelize
.
define
(
'ModelWithFieldPK'
,
{
userId
:
{
field
:
'user_id'
,
type
:
Sequelize
.
INTEGER
,
autoIncrement
:
true
,
primaryKey
:
true
},
foo
:{
type
:
DataTypes
.
STRING
,
unique
:
true
}
});
return
this
.
sequelize
.
sync
({
force
:
true
});
return
this
.
sequelize
.
sync
({
force
:
true
});
});
});
...
@@ -226,6 +239,30 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -226,6 +239,30 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
});
});
it
(
'works with primary key using .field'
,
function
()
{
return
this
.
ModelWithFieldPK
.
upsert
({
userId
:
42
,
foo
:
'first'
}).
bind
(
this
).
then
(
function
(
created
)
{
if
(
dialect
===
'sqlite'
)
{
expect
(
created
).
to
.
be
.
undefined
;
}
else
{
expect
(
created
).
to
.
be
.
ok
;
}
return
this
.
sequelize
.
Promise
.
delay
(
1000
).
bind
(
this
).
then
(
function
()
{
return
this
.
ModelWithFieldPK
.
upsert
({
userId
:
42
,
foo
:
'second'
});
});
}).
then
(
function
(
created
)
{
if
(
dialect
===
'sqlite'
)
{
expect
(
created
).
to
.
be
.
undefined
;
}
else
{
expect
(
created
).
not
.
to
.
be
.
ok
;
}
return
this
.
ModelWithFieldPK
.
findOne
({
userId
:
42
});
}).
then
(
function
(
instance
)
{
expect
(
instance
.
foo
).
to
.
equal
(
'second'
);
});
});
it
(
'works with database functions'
,
function
()
{
it
(
'works with database functions'
,
function
()
{
return
this
.
User
.
upsert
({
id
:
42
,
username
:
'john'
,
foo
:
this
.
sequelize
.
fn
(
'upper'
,
'mixedCase1'
)}).
bind
(
this
).
then
(
function
(
created
)
{
return
this
.
User
.
upsert
({
id
:
42
,
username
:
'john'
,
foo
:
this
.
sequelize
.
fn
(
'upper'
,
'mixedCase1'
)}).
bind
(
this
).
then
(
function
(
created
)
{
if
(
dialect
===
'sqlite'
)
{
if
(
dialect
===
'sqlite'
)
{
...
...
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