This project is archived and is in readonly mode.
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
-
François Beausoleil April 9th, 2009 @ 07:20 PM
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.
-
Jon Leighton December 16th, 2010 @ 08:58 AM
- 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
inhas_many_through_associations_test.rb
andtest_eager_load_has_many_through_quotes_table_and_column_names
ineager_test.rb
. -
Jon Leighton December 20th, 2010 @ 08:11 PM
- State changed from new to resolved
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>