Skip to content

fix(describe-table-query): add condition to prevent subquery error - #12568

Closed
nachocab wants to merge 1 commit into
sequelize:masterfrom
nachocab:master
Closed

nachocab wants to merge 1 commit into
sequelize:masterfrom
nachocab:master

Conversation

@nachocab

@nachocab nachocab commented Jul 29, 2020 •

Copy link
Copy Markdown

Pull Request check-list

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

  • 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 update the typescript typings accordingly (if applicable)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

If there is more than one schema with the same table names (e. g.: public and backup), the pgd.description subquery in describeTableQuery will return more than one row and will result in the error SequelizeDatabaseError: more than one row returned by a subquery used as an expression.

The added condition prevents this from happening.

@codecov

codecov Bot commented Jul 29, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #12568 into master will increase coverage by 6.17%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #12568      +/-   ##
==========================================
+ Coverage   90.13%   96.30%   +6.17%     
==========================================
  Files          92       95       +3     
  Lines        9112     9256     +144     
  Branches       90       90              
==========================================
+ Hits         8213     8914     +701     
+ Misses        880      325     -555     
+ Partials       19       17       -2     
Impacted Files Coverage Δ
src/dialects/postgres/query-generator.js 94.73% <ø> (ø)
src/dialects/mssql/index.js 100.00% <0.00%> (ø)
src/dialects/mssql/query-interface.js 100.00% <0.00%> (ø)
src/dialects/mssql/connection-manager.js 87.17% <0.00%> (ø)
src/model.js 96.58% <0.00%> (+0.06%) ⬆️
src/sequelize.js 95.54% <0.00%> (+0.63%) ⬆️
src/dialects/abstract/query-generator.js 97.41% <0.00%> (+1.00%) ⬆️
src/sql-string.ts 85.93% <0.00%> (+1.56%) ⬆️
...dialects/abstract/query-generator/helpers/quote.js 100.00% <0.00%> (+6.66%) ⬆️
src/errors/database/unknown-constraint-error.ts 90.00% <0.00%> (+10.00%) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d06aa03...67bd2bb. Read the comment docs.

@nachocab

nachocab commented Aug 7, 2020 •

Copy link
Copy Markdown
Author

Any updates on this? It seems like a pretty simple merge

@dmercertaylor

Copy link
Copy Markdown

It looks like one of the key maintainers on this project just left, so it might take the remaining team a minute to get to these. It looks like the PRs have been piling up for the last little bit.

@nachocab

nachocab commented Dec 9, 2020

Copy link
Copy Markdown
Author

It would be great if someone could review this 😅

@sdepold

sdepold commented Oct 24, 2021

Copy link
Copy Markdown
Member

Hey there :)

First of all: Thanks a bunch for your contribution to Sequelize! Much appreciated!
Second: Unfortunately we haven't had the chance to look into this ever since you created it. Sorry for that!

A couple of months ago, we have switched from master to main branch as our primary development branch and hence this PR is now outdated :(

If you still think this change is making sense, please consider recreating the PR against main. Thanks in advance and sorry for the additional work.

✌️

@github-actions github-actions Bot added the stale label Nov 2, 2021
@github-actions github-actions Bot closed this Nov 8, 2021
@jiqiang90

Copy link
Copy Markdown

Hi guys we also facing the same issue, any update on this?

@WikiRik

WikiRik commented Sep 21, 2022

Copy link
Copy Markdown
Member

Feel free to open a new PR to include this, we would need additional tests. At least one for the normal behaviour that already works on main and one that doesn't work on main (so multiple schemas with the same table name) and is fixed with this change.
What you probably would want to test is if the describeTable function of the query interface is correct. We have integration tests on that here but seeing as this change is only for postgres it might be worth putting them here.
Although it might be nicer if you could make tests that are done on all dialects that support multiple schemas.

@ephys you're working on schemas as part of #14999 so maybe you can give additional pointers or take it up yourself

@dorlanpabon dorlanpabon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is appropriate and necessary to improve the accuracy and reliability of queries generated by Sequelize.

Prevents subquery errors: By adding the condition AND c.table_schema = st.schemaname, this ensures that the subquery returns results only from the correct schema, preventing the "more than one row returned by a subquery used as an expression" error, which occurs when there are tables with the same name in different schemas.

Clarity and accuracy: This change makes the query more precise by ensuring that the information is fetched from the correct schema, which is crucial in databases with multiple schemas or complex configurations.

Avoids unexpected behavior: Without this condition, the query could produce unpredictable results in environments with multiple schemas. Adding this check ensures more robust and reliable behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants