Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Joined table not quoted with has_many :through

#2461

Given the following models:


class EmbedCode < AR::B
  has_many :references
  has_many :orders, :through => :references
end

class Reference < AR::B
  belongs_to :embed_code
  belongs_to :order
end

class Order < AR::B
end

The following will fail under both MySQL and SQLite3:


EmbedCode.first.orders.count

The exact messages are:


sqlite> SELECT count(*) AS count_all FROM "orders"  INNER JOIN references ON orders.id = references.order_id    WHERE (("references".embed_code_id = 664786516));
SQL error: near "references": syntax error

mysql> SELECT count(*) AS count_all FROM `orders`  INNER JOIN references ON orders.id = references.order_id    WHERE ((`references`.embed_code_id = 664786516)); 
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'references ON orders.id = references.order_id    WHERE ((`references`.embed_code' at line 1

The problem is the naked references in the INNER JOIN clause. References is a reserved word in SQL-land.

Reported by François Beausoleil · April 8th, 2009 @ 09:10 PM

State: resolved
Milestone: 3.x
Assigned to: Aaron Patterson Aaron Patterson
Importance: none

Activity

  1. François Beausoleil
    François Beausoleil

    mynyml attempted to reproduce on 2.3.2 and it was fixed: http://gist.github.com/92224

    I had the bug on 2.2.2.

    April 9th, 2009 @ 07:20 PM

  2. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  3. Jon Leighton
    Jon Leighton
    • Assigned user set to Aaron Patterson
    • Importance changed from to

    This is fixed in master so it can be closed. The behaviour is captured by test_inner_join_with_quoted_table_name in has_many_through_associations_test.rb and test_eager_load_has_many_through_quotes_table_and_column_names in eager_test.rb.

    December 16th, 2010 @ 08:58 AM

  4. Jon Leighton
    Jon Leighton
    • State changed from new to resolved

    December 20th, 2010 @ 08:11 PM