Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(model): don't add LIMIT in findOne() queries on unique key #9248

merged 1 commit into from Apr 6, 2018


Copy link

@jharting jharting commented Mar 28, 2018

Pull Request check-list

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in


When querying with findOne() Sequelize adds LIMIT 1 to the query to limit the result size. In combination with duplicating joins this may result in a complicated query (subqueryFilter). An optimization exists in Sequelize where if the query's where clause contains the primary key then LIMIT 1 is skipped.

Description of change

This change extends the optimization to also cover unique columns.

Copy link

@codecov codecov bot commented Mar 28, 2018

Codecov Report

Merging #9248 into master will increase coverage by <.01%.
The diff coverage is 100%.

@jharting jharting force-pushed the jharting:find-one-unique-column branch from 6951dce to b6d212b Apr 6, 2018
@@ -1737,10 +1737,13 @@ class Model {
options = Utils.cloneDeep(options);

if (options.limit === undefined) {
const pkVal = options.where && options.where[this.primaryKeyAttribute];
const uniqueSingleColumns = _(this.uniqueKeys).values().filter(c => c.fields.length === 1).map('column').value();

This comment has been minimized.


janmeier Apr 6, 2018

use .chain here to make it explicit what's happening

@jharting jharting force-pushed the jharting:find-one-unique-column branch from b6d212b to 819dfa2 Apr 6, 2018
@janmeier janmeier merged commit 0709226 into sequelize:master Apr 6, 2018
4 checks passed
4 checks passed
codecov/patch 100% of diff hit (target 95.91%)
codecov/project 95.91% (+<.01%) compared to e8d5720
continuous-integration/appveyor/pr AppVeyor build succeeded
continuous-integration/travis-ci/pr The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants