feat: Always add LIMIT 1 to `findOne` queries by rharriso · Pull Request #14549 · sequelize/sequelize · GitHub
Skip to content

feat: Always add LIMIT 1 to findOne queries - #14549

Merged
WikiRik merged 3 commits into
sequelize:mainfrom
rharriso:bugfix/always-limit-findOne
Jun 14, 2022
Merged

feat: Always add LIMIT 1 to findOne queries#14549
WikiRik merged 3 commits into
sequelize:mainfrom
rharriso:bugfix/always-limit-findOne

Conversation

@rharriso

Copy link
Copy Markdown
Contributor

Pull Request Checklist

Please make sure to review and check all of these items:

  • Have you added new tests to prevent regressions? No, tests remove
  • Does yarn test or yarn test-DIALECT pass with this change (including linting)?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)? No this removes an undocumented behavior
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description Of Change

Remove conditional logic for adding LIMIT 1 to findOne queries. Instead, always add it.

  • Foreign keys should be used to ensure data integrity, and should not impact query performance.
  • It's unclear how omitting LIMIT 1 benefits any query that should return a single result.
  • Models are imperfect representations of schema, making unnecessarily assumptions about their correctness can lead to severe issues.

@kpeluso

kpeluso commented May 25, 2022

Copy link
Copy Markdown

@ephys

ephys commented May 25, 2022

Copy link
Copy Markdown
Member

I agree, not adding LIMIT 1 if we're comparing the primary key sounds weird to me. I'd expect Model.findOne() to be equivalent to (await Model.findAll({ limit: 1 }))[0].

Strangely enough, some mssql tests are failing with this change.
If you could ping me once these tests are passing (or if you need help), I'll review this PR once they're green (db2 tests are unstable and can be ignored)

This is the only comment I could find that mentioned this behavior as intended #4416 (comment)

@ephys
ephys self-requested a review May 25, 2022 18:52
@rharriso

Copy link
Copy Markdown
Contributor Author

@ephys thanks for looking a this. I reran the mssql tests locally on main and observed the same error. I'll check through the commit history and see if I can't find if it broke with a prior commit.

@rharriso

rharriso commented Jun 3, 2022

Copy link
Copy Markdown
Contributor Author

@ephys , sorry to say I have been able to identify a commit where these tests started failing. I'm wondering if that has come up in other PRs?

To Reiterate, the MSSQL tests fail locally for me on main

@ephys

ephys commented Jun 3, 2022

Copy link
Copy Markdown
Member

are you using start-mssql to start the server?

@rharriso
rharriso force-pushed the bugfix/always-limit-findOne branch 3 times, most recently from ac96380 to fcbea07 Compare June 3, 2022 15:55
@rharriso

rharriso commented Jun 3, 2022

Copy link
Copy Markdown
Contributor Author

@ephys, I'm trying to make sure I'm properly running the test locally.
I'll get back to you once I'm certain that I'm getting the same results as CI.

Thanks for your patience.

Comment thread src/model.js
@rharriso

Copy link
Copy Markdown
Contributor Author

I've added the conditional pointed out by @WikiRik, and opened up issue #14618

@rharriso
rharriso force-pushed the bugfix/always-limit-findOne branch from fe5747a to c7f106a Compare June 10, 2022 18:09
@ephys

ephys commented Jun 11, 2022

Copy link
Copy Markdown
Member

I found the reason why LIMIT was not included #9248. I'm documenting it here for future PRs but I disagree with the premise of that PR and I agree LIMIT should be specified.

Edit: Changed the commit type to feat as it was an intended behavior we're changing instead of a bug

@ephys ephys changed the title Bugfix: Always add LIMIT 1 to findOne queries feat: Always add LIMIT 1 to findOne queries Jun 11, 2022
ephys
ephys previously approved these changes Jun 11, 2022
@WikiRik

WikiRik commented Jun 13, 2022

Copy link
Copy Markdown
Member

I'll run the tests again after GitHub solves the caching outage https://www.githubstatus.com/incidents/6r4sms3hcj3x

@WikiRik
WikiRik merged commit 9950b4b into sequelize:main Jun 14, 2022
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 7.0.0-alpha.14 🎉

The release is available on:

Your semantic-release bot 📦🚀

@vanthome

Copy link
Copy Markdown
Contributor

Hello, I have my doubts that this is correct. Are we sure that findOne queries should really only ever return one row? We are using findOne in conjunction with includes and then, because of outer joins happening, we do get and expect multiple rows. Please correct me if I'm wrong. Thx

@ephys

ephys commented Oct 17, 2022

Copy link
Copy Markdown
Member

abel-matsec pushed a commit to abel-matsec/sequelize that referenced this pull request Feb 28, 2025
* fix: always add LIMIT 1 to `findOne` queries

* fix: add temp patch for issue 14618

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants