Skip to content

Eager load order bug with Arel joins after update to Rails 6 #38354

@diei

Description

@diei

I updated Rails from 5.2.4.1 to 6.0.2.1 and now some SQL queries with Arel parts are not running any more. It seems to be similar to #37222.

Steps to reproduce

class Magazine < ActiveRecord::Base
  belongs_to :publisher, foreign_key: :publisher_id
  has_many :issues
end
class Issue < ActiveRecord::Base
  belongs_to :magazine
end
class Publisher < Client
  has_many :magazines, foreign_key: :publisher_id
  has_many :issues, through: :magazines
end
class Client < ActiveRecord::Base
end

publishers_table = Publisher.arel_table
magazines_table = Magazine.arel_table
publisher_join = magazines_table.join(publishers_table, Arel::Nodes::OuterJoin).
    on(publishers_table[:id].eq(magazines_table[:publisher_id])).join_sources

Issue.eager_load(:magazine).joins(publisher_join).where(publishers_table[:name].eq('my publisher'))

Expected behavior

I expect that all issues from the publisher 'my publisher' are returned and to each issue the magazine is eager loaded.

Actual behavior

I get following error:
ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'magazines.publisher_id' in 'on clause')

The join order is switched and therefore magazines is unknown.

System configuration

Rails version:
Rails 6.0.2.1

Ruby version:
ruby-2.6.5

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions