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 (, 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(}_#{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, 

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=""></a>

People watching this ticket


Referenced by