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

ActiveRecord left joins out of order #32598

Closed
ryan-plated opened this issue Apr 17, 2018 · 15 comments
Closed

ActiveRecord left joins out of order #32598

ryan-plated opened this issue Apr 17, 2018 · 15 comments

Comments

@ryan-plated
Copy link

ryan-plated commented Apr 17, 2018

Steps to reproduce

Combine sql left join with a arel join causes wrong order

https://gist.github.com/ryan-plated/cae50e8eceb621f8a76d41f2e6d1b5ec

Expected behavior

should keep order

Actual behavior

goes out of order

System configuration

Rails version: master

Ruby version: 2.5.0

@AlexKovynev
Copy link

@ryan-plated ryan-plated changed the title ActiveRecord joins out of order ActiveRecord left joins out of order Apr 18, 2018
@ryan-plated
Copy link
Author

I am not sure what you are trying to communicate. I read the post and it says outer joins order does matter.

@AlexKovynev
Copy link

Read post carefully, order of params does matter (tables, columns), not joins itself.

@rafaelfranca
Copy link
Member

Could you explain better the expected behavior and the actual behavior? "should keep order" doesn't tell anything to me. Order of what?

@rafaelfranca
Copy link
Member

I think it is related to #15461

@ryanong
Copy link
Contributor

ryanong commented Apr 20, 2018

YEA that is exactly it! I'll gladly pick up the stale PR! I just didn't even know where to start.

@heaven
Copy link

heaven commented Apr 26, 2018

Here is our example that raises the Unknown column error:

scope :expired, ->(service, time = 1.month) {
  distinct.
    left_joins(:timestamps).
    joins("LEFT JOIN timestamp_kinds...")
}

@ryan-plated
Copy link
Author

This might take a while to fix. Right now joins bucketed together.

def build_join_query(manager, buckets, join_type, aliases)

String joins may or may not have a left join contained in them so they may need to keep order with outer joins.

@rails-bot rails-bot bot added the stale label Jul 26, 2018
@rails-bot
Copy link

rails-bot bot commented Jul 26, 2018

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-2-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot closed this as completed Aug 2, 2018
@ismailallocator
Copy link

This is still an issue

left_joins(:funds) .joins("LEFT JOIN visible_funds ON funds.id = visible_funds.fund_id AND visible_funds.company_id = #{company.id}")

returns error.

@rails-bot rails-bot bot removed the stale label Sep 27, 2018
@dgmora
Copy link

dgmora commented Oct 30, 2018

Is there an active PR/issue for this (still existing) issue? #15461 is closed and that one links to #24281, which is kinda related but is not the same, I think

@hassanrehman
Copy link

+1 for PR

@mateuscruz
Copy link

I'm willing to create a PR for this. I ran into this issue earlier today.

@khiav223577
Copy link

khiav223577 commented Jun 6, 2020

Did this bug be fixed? I ran into this issue, too. (Rails 5.2.4)

@kamipo
Copy link
Member

kamipo commented Jun 6, 2020

Fixed by #35864 in Rails 6.0.

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

No branches or pull requests