Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix distinct alias when multiple databases used #1262

Merged
merged 3 commits into from
Nov 23, 2024

Conversation

aidanharan
Copy link
Contributor

@aidanharan aidanharan commented Nov 21, 2024

Fix issue with the generated SQL for DISTINCT queries as reported in #1256. The generated SQL:

  • Unnecessarily added a ALIAS AS projection.
  • The column used of the ALIAS AS was being incorrectly quoted. This was caused by MySQL database also being used in multi database Rails application.
Post.includes(:comments).joins(:comments).first

Before:

SELECT DISTINCT [posts].[id] AS alias_0, [posts].[id] FROM [posts] INNER JOIN [comments] ON [comments].[post_id] = [posts].[id] ORDER BY [posts].[id] ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY

After:

SELECT DISTINCT [posts].[id] FROM [posts] INNER JOIN [comments] ON [comments].[post_id] = [posts].[id] ORDER BY [posts].[id] ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY

@aidanharan aidanharan changed the title Fix issue with distinct alias when multiple databases used Fix distinct alias when multiple databases used Nov 21, 2024
@aidanharan aidanharan marked this pull request as ready for review November 23, 2024 09:56
@aidanharan aidanharan merged commit 2ba0c8a into main Nov 23, 2024
4 checks passed
@aidanharan aidanharan deleted the remove-distinct-as-alias branch November 23, 2024 20:40
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant