This project is archived and is in readonly mode.

#6423 ✓committed
Ernie Miller

[PATCH] fix table name collision on certain tricky association joins

Reported by Ernie Miller | February 12th, 2011 @ 07:24 PM

In master, there's a regression in the new association handling cod. Way back in a patch I submitted to 3.0 to fix similar problems with eager loading (https://github.com/rails/rails/commit/e33d304975f5b20b0ba819ab644a2..., there was another edge case that got "accidentally" fixed as well.

The issue is this:

In JoinAssociation#aliased_table_name_for, the following code is run to generate an aliased table name:

name = connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}"
table_index = aliases[name] + 1
name = name[0, connection.table_alias_length-3] + "_#{table_index}" if table_index > 1

After this, aliases[name] is incremented. But since the value of "name" is now the new, aliased table name, the effect is that we end up with an aliases hash that looks something like this (taken from a multiply self-referential parent-child join:

{
  "people"=>1, "children_people"=>1,
  "children_people_2"=>2, "parents_people"=>1, 
  "parents_people_2"=>2
}

As you can see, instead of counting aliases against children_people, we began counting against the aliased table name itself. This leads the JoinDependency to create invalid JoinAssociations (2 against children_people_2 and parents_people_2 in this case, which means twice it incremented children_people and landed on the same table name), and creates table name collisions.

This patch fixes the regression and adds a test to cover it in the future.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Referenced by

Pages