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

Incorrect Join Order for Multiple ActiveRecord Merges #27887

Closed
niedfelj opened this issue Feb 2, 2017 · 3 comments
Closed

Incorrect Join Order for Multiple ActiveRecord Merges #27887

niedfelj opened this issue Feb 2, 2017 · 3 comments

Comments

@niedfelj
Copy link

niedfelj commented Feb 2, 2017

Steps to reproduce

When attempting to merge multiple relations together, the last one below incorrectly generates joins that are out of order and throw an error in Postgres. Not sure if this is related to #25806. There seem to be a few open issues that have not been resolved that are similar to this one.

location_filter = Location.where(@location_wheres.join(' AND '))
user_filter = User.opt_in.where("birthday >= ? and birthday <= ? #{@birthday_null}", Time.now() - @end_age.years, Time.now() - @start_age.years).where(@gender).joins(:location).merge(location_filter)
@all_list_items = account_group.list_items.joins(:contributor_user).merge(user_filter)
@all_lists = account_group.lists.merge(@all_list_items)

All of the queries except for the last one are successful:

!! #<ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "users"
LINE 1: ...LEFT OUTER JOIN "locations" ON "locations"."id" = "users"."l...
                                                             ^
: SELECT "lists".* FROM "lists" LEFT OUTER JOIN "locations" ON "locations"."id" = "users"."location_id" LEFT OUTER JOIN "users" ON "users"."id" = "list_items"."contributor_user_id" INNER JOIN "list_items" ON "lists"."id" = "list_items"."list_id" WHERE (list_status_code!='Deleted') AND "list_items"."domain" = $1 AND "list_items"."domain" = $2 AND "users"."opt_out" = $3 AND (birthday >= '1817-02-02 18:03:12.184201' and birthday <= '2017-02-02 18:03:12.184290'  or birthday is null) AND (1=1) AND (city='Brooklyn')>

Expected behavior

It should do the joins in the correct order.

Actual behavior

It joins in the wrong order and throws an error.

System configuration

Rails version: 5.0.1

Ruby version: 2.3.3

@eileencodes
Copy link
Member

Can you recreate this error using our bug report template? That will make it easier for us to track down the bug you're seeing.

@rails-bot
Copy link

rails-bot bot commented May 6, 2017

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-1-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 May 14, 2017
@aleksandrs-ledovskis
Copy link

Tracked #24281

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

4 participants