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 e319b6cf
authored
Sep 30, 2015
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug(findOne) Add limit when querying for a range on the primary key. Closes #4416
1 parent
803f3532
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
85 additions
and
5 deletions
changelog.md
lib/instance.js
lib/model.js
lib/utils.js
test/unit/model/findone.test.js
changelog.md
View file @
e319b6c
...
...
@@ -6,6 +6,7 @@
-
[
INTERNALS
]
Updated devDependencies
[
#4594
](
https://github.com/sequelize/sequelize/pull/4594
)
+
mysql@2.9.0
-
coffee-script
-
[
FIXED
]
Add limit to
`findOne`
when using queries like
`{ id: { $gt ...`
[
#4416
](
https://github.com/sequelize/sequelize/issues/4416
)
# 3.10.0
-
[
ADDED
]
support
`search_path`
for postgres with lots of schemas
[
#4534
](
https://github.com/sequelize/sequelize/pull/4534
)
...
...
lib/instance.js
View file @
e319b6c
...
...
@@ -8,7 +8,6 @@ var Utils = require('./utils')
,
Dottie
=
require
(
'dottie'
)
,
Promise
=
require
(
'./promise'
)
,
_
=
require
(
'lodash'
)
,
primitives
=
[
'string'
,
'number'
,
'boolean'
]
,
defaultsOptions
=
{
raw
:
true
};
// private
...
...
@@ -159,7 +158,7 @@ Instance.prototype.getDataValue = function(key) {
*/
Instance
.
prototype
.
setDataValue
=
function
(
key
,
value
)
{
var
originalValue
=
this
.
_previousDataValues
[
key
];
if
(
primitives
.
indexOf
(
typeof
value
)
===
-
1
||
value
!==
originalValue
)
{
if
(
!
Utils
.
isPrimitive
(
value
)
||
value
!==
originalValue
)
{
this
.
changed
(
key
,
true
);
}
...
...
@@ -373,7 +372,7 @@ Instance.prototype.set = function(key, value, options) { // testhint options:non
value
=
this
.
Model
.
attributes
[
key
].
type
.
parse
(
value
);
}
if
(
!
options
.
raw
&&
((
primitives
.
indexOf
(
typeof
value
)
===
-
1
&&
value
!==
null
)
||
value
!==
originalValue
))
{
if
(
!
options
.
raw
&&
((
!
Utils
.
isPrimitive
(
value
)
&&
value
!==
null
)
||
value
!==
originalValue
))
{
this
.
_previousDataValues
[
key
]
=
originalValue
;
this
.
changed
(
key
,
true
);
}
...
...
lib/model.js
View file @
e319b6c
...
...
@@ -1468,9 +1468,14 @@ Model.prototype.findOne = function(options) {
}
options
=
optClone
(
options
);
if
(
options
.
limit
===
undefined
&&
!
(
options
.
where
&&
options
.
where
[
this
.
primaryKeyAttribute
]))
{
if
(
options
.
limit
===
undefined
)
{
var
pkVal
=
options
.
where
&&
options
.
where
[
this
.
primaryKeyAttribute
];
// Don't add limit if querying directly on the pk
if
(
!
options
.
where
||
!
(
Utils
.
isPrimitive
(
pkVal
)
||
Buffer
.
isBuffer
(
pkVal
)))
{
options
.
limit
=
1
;
}
}
// Bypass a possible overloaded findAll.
return
Model
.
prototype
.
findAll
.
call
(
this
,
_
.
defaults
(
options
,
{
...
...
lib/utils.js
View file @
e319b6c
...
...
@@ -6,7 +6,8 @@ var DataTypes = require('./data-types')
,
parameterValidator
=
require
(
'./utils/parameter-validator'
)
,
inflection
=
require
(
'inflection'
)
,
uuid
=
require
(
'node-uuid'
)
,
deprecate
=
require
(
'depd'
)(
'Utils'
);
,
deprecate
=
require
(
'depd'
)(
'Utils'
)
,
primitives
=
[
'string'
,
'number'
,
'boolean'
];
var
Utils
=
module
.
exports
=
{
inflection
:
inflection
,
...
...
@@ -29,6 +30,9 @@ var Utils = module.exports = {
return
result
;
},
isPrimitive
:
function
(
val
)
{
return
primitives
.
indexOf
(
typeof
val
)
!==
-
1
;
},
// Same concept as _.merge, but don't overwrite properties that have already been assigned
mergeDefaults
:
function
(
a
,
b
)
{
return
this
.
_
.
merge
(
a
,
b
,
function
(
objectValue
,
sourceValue
)
{
...
...
test/unit/model/findone.test.js
0 → 100644
View file @
e319b6c
'use strict'
;
/* jshint -W030 */
var
chai
=
require
(
'chai'
)
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../support'
)
,
current
=
Support
.
sequelize
,
sinon
=
require
(
'sinon'
)
,
DataTypes
=
require
(
__dirname
+
'/../../../lib/data-types'
)
,
Promise
=
require
(
'bluebird'
);
describe
(
Support
.
getTestDialectTeaser
(
'Model'
),
function
()
{
describe
(
'method findOne'
,
function
()
{
before
(
function
()
{
this
.
oldfindAll
=
current
.
Model
.
prototype
.
findAll
;
});
after
(
function
()
{
current
.
Model
.
prototype
.
findAll
=
this
.
oldfindall
;
});
beforeEach
(
function
()
{
this
.
stub
=
current
.
Model
.
prototype
.
findAll
=
sinon
.
stub
().
returns
(
Promise
.
resolve
());
});
describe
(
'should not add limit when querying on a primary key'
,
function
()
{
it
(
'with id primary key'
,
function
()
{
var
Model
=
current
.
define
(
'model'
);
return
Model
.
findOne
({
where
:
{
id
:
42
}}).
bind
(
this
).
then
(
function
()
{
expect
(
this
.
stub
.
getCall
(
0
).
args
[
0
]).
to
.
be
.
an
(
'object'
).
not
.
to
.
have
.
property
(
'limit'
);
});
});
it
(
'with custom primary key'
,
function
()
{
var
Model
=
current
.
define
(
'model'
,
{
uid
:
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
}
});
return
Model
.
findOne
({
where
:
{
uid
:
42
}}).
bind
(
this
).
then
(
function
()
{
expect
(
this
.
stub
.
getCall
(
0
).
args
[
0
]).
to
.
be
.
an
(
'object'
).
not
.
to
.
have
.
property
(
'limit'
);
});
});
it
(
'with blob primary key'
,
function
()
{
var
Model
=
current
.
define
(
'model'
,
{
id
:
{
type
:
DataTypes
.
BLOB
,
primaryKey
:
true
,
autoIncrement
:
true
}
});
return
Model
.
findOne
({
where
:
{
id
:
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 { $ gt on the primary key'
,
function
()
{
var
Model
=
current
.
define
(
'model'
);
return
Model
.
findOne
({
where
:
{
id
:
{
$gt
:
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