This project is archived and is in readonly mode.

#2461 ✓resolved
François Beausoleil

Joined table not quoted with has_many :through

Reported by François Beausoleil | April 8th, 2009 @ 09:10 PM | in 3.x

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.

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>

Pages