This project is archived and is in readonly mode.

#364 ✓invalid
Dimitrij Denissenko

Eager loading does not quote the table names properly

Reported by Dimitrij Denissenko | June 7th, 2008 @ 04:35 PM

Tested with MySQL & SQLite3, both fail. Example:

class Group < ActiveRecord::Base
  set_table_name 'g-r-o-u-p-s'
  has_many :permissions
end
class Permission < ActiveRecord::Base
  set_table_name 'per-missions'
end
Group.find 1, :include => [:permissions]

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: s.id: SELECT "g-r-o-u-p-s"."id" AS t0_r0, "g-r-o-u-p-s"."name" AS t0_r1, "g-r-o-u-p-s"."created_at" AS t0_r2, "g-r-o-u-p-s"."updated_at" AS t0_r3, "per-missions"."id" AS t1_r0, "per-missions"."name" AS t1_r1, "per-missions"."group_id" AS t1_r2, "per-missions"."created_at" AS t1_r3, "per-missions"."updated_at" AS t1_r4 FROM "g-r-o-u-p-s"  LEFT OUTER JOIN "per-missions" ON per-missions.group_id = g-r-o-u-p-s.id     WHERE ("g-r-o-u-p-s"."id" = 1) 
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:147:in `log'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:132:in `execute'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:360:in `catch_schema_changes'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:132:in `execute'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:263:in `select'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `select_all'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations.rb:1417:in `select_all_rows'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations.rb:1250:in `find_with_associations'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations.rb:1248:in `catch'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/associations.rb:1248:in `find_with_associations'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1339:in `find_every'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1376:in `find_one'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1362:in `find_from_ids'
	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:537:in `find'
	from (irb):6

Proposed patch:

--- associations.rb	2008-06-07 10:27:27.000000000 +0100
+++ associations.rb	2008-06-07 16:33:32.000000000 +0100
@@ -1903,9 +1903,9 @@
                       foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
                       " #{join_type} %s ON %s.%s = %s.%s " % [
                         table_name_and_alias,
-                        aliased_table_name,
+                        connection.quote_table_name(aliased_table_name),
                         foreign_key,
-                        parent.aliased_table_name,
+                        connection.quote_table_name(parent.aliased_table_name),
                         parent.primary_key
                       ]
                   end
@@ -1957,7 +1957,7 @@
               end
 
               def table_alias_for(table_name, table_alias)
-                 "#{reflection.active_record.connection.quote_table_name(table_name)} #{table_alias if table_name != table_alias}".strip
+                 "#{reflection.active_record.connection.quote_table_name(table_name)} #{reflection.active_record.connection.quote_table_name(table_alias) if table_name != table_alias}".strip
               end
 
               def table_name_and_alias

Comments and changes to this ticket

  • Pratik

    Pratik July 23rd, 2008 @ 02:13 PM

    • State changed from “new” to “invalid”
    • Tag set to 2.1.0, activerecord, bug, eager_loading, has_many

    Looks like this is fixed. If not, please upload a diff file using git-format-patch and with tests.

    Thanks.

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

Pages