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 07092263
authored
Apr 06, 2018
by
Jozef Hartinger
Committed by
Jan Aagaard Meier
Apr 06, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(model): don't add LIMIT in findOne() queries on unique key (#9248)
1 parent
e8d57201
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
3 deletions
lib/model.js
test/unit/model/findone.test.js
lib/model.js
View file @
0709226
...
...
@@ -1737,10 +1737,13 @@ class Model {
options
=
Utils
.
cloneDeep
(
options
);
if
(
options
.
limit
===
undefined
)
{
const
pkVal
=
options
.
where
&&
options
.
where
[
this
.
primaryKeyAttribute
]
;
const
uniqueSingleColumns
=
_
.
chain
(
this
.
uniqueKeys
).
values
().
filter
(
c
=>
c
.
fields
.
length
===
1
).
map
(
'column'
).
value
()
;
// Don't add limit if querying directly on the pk
if
(
!
options
.
where
||
!
(
Utils
.
isPrimitive
(
pkVal
)
||
Buffer
.
isBuffer
(
pkVal
)))
{
// Don't add limit if querying directly on the pk or a unique column
if
(
!
options
.
where
||
!
_
.
some
(
options
.
where
,
(
value
,
key
)
=>
(
key
===
this
.
primaryKeyAttribute
||
_
.
includes
(
uniqueSingleColumns
,
key
))
&&
(
Utils
.
isPrimitive
(
value
)
||
Buffer
.
isBuffer
(
value
))
))
{
options
.
limit
=
1
;
}
}
...
...
test/unit/model/findone.test.js
View file @
0709226
...
...
@@ -67,5 +67,49 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
describe
(
'should not add limit when querying on an unique key'
,
()
=>
{
it
(
'with custom unique key'
,
function
()
{
const
Model
=
current
.
define
(
'model'
,
{
unique
:
{
type
:
DataTypes
.
INTEGER
,
unique
:
true
}
});
return
Model
.
findOne
({
where
:
{
unique
:
42
}}).
bind
(
this
).
then
(
function
()
{
expect
(
this
.
stub
.
getCall
(
0
).
args
[
0
]).
to
.
be
.
an
(
'object'
).
not
.
to
.
have
.
property
(
'limit'
);
});
});
it
(
'with blob unique key'
,
function
()
{
const
Model
=
current
.
define
(
'model'
,
{
unique
:
{
type
:
DataTypes
.
BLOB
,
unique
:
true
}
});
return
Model
.
findOne
({
where
:
{
unique
:
new
Buffer
(
'foo'
)
}}).
bind
(
this
).
then
(
function
()
{
expect
(
this
.
stub
.
getCall
(
0
).
args
[
0
]).
to
.
be
.
an
(
'object'
).
not
.
to
.
have
.
property
(
'limit'
);
});
});
});
it
(
'should add limit when using multi-column unique key'
,
function
()
{
const
Model
=
current
.
define
(
'model'
,
{
unique1
:
{
type
:
DataTypes
.
INTEGER
,
unique
:
'unique'
},
unique2
:
{
type
:
DataTypes
.
INTEGER
,
unique
:
'unique'
}
});
return
Model
.
findOne
({
where
:
{
unique1
:
42
}}).
bind
(
this
).
then
(
function
()
{
expect
(
this
.
stub
.
getCall
(
0
).
args
[
0
]).
to
.
be
.
an
(
'object'
).
to
.
have
.
property
(
'limit'
);
});
});
});
});
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