This project is archived and is in readonly mode.
table_name_prefix not honored under rake test
Reported by Fredrik Thuresson | February 9th, 2011 @ 07:14 PM
Creating a brand new app under Rails 3.0.4 with one model in it,
config/database.yml
development:
database: bogus
test:
database: bogus_test
The table exists in products database
class Item < ActiveRecord::Base
def self.table_name_prefix
'products.'
end
end
the table_name_prefix is not honered when running rake test. It works fine in console and in browser to pick it up from the products database.
rake test produces
Mysql2::Error: Table 'bogus_test.items' doesn't exist: DELETE FROM
items
Is this expected?
Comments and changes to this ticket
-
kenmazaika February 14th, 2011 @ 01:35 AM
I do not believe MySQL supports having periods in table names, because if it did queries could be ambiguous with federated tables:
mysql> create table something.test (id INT);
ERROR 1049 (42000): Unknown database 'something'
mysql> create table something_test (id INT);
Query OK, 0 rows affected (0.00 sec)I suspect this will work with other database engines though.
-ken
-
Fredrik Thuresson March 16th, 2011 @ 02:22 PM
kenmazaika: The period is separating the database and the table name.
Database: bogus_test, Table Name: itemsThe following worked for me in test/test_helper.rb
Replaced
@connection = class_name.connection if class_name.respond_to?(:connection) with
@connection = table_name.classify.constantize.connectionclass Fixtures
def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter @name = table_name # preserve fixture base name @class_name = class_name || (ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize) @table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}" @table_name = class_name.table_name if class_name.respond_to?(:table_name) @connection = table_name.classify.constantize.connection read_fixture_files
end end
-Fredrik
-
Fredrik Thuresson March 16th, 2011 @ 02:25 PM
Obviously I don't know how to format. Here is another attempt. Ignore the previous comment.
kenmazaika: The period is separating the database and the table name.
Database: bogus_test, Table Name: items
The following worked for me in test/test_helper.rbReplaced @connection = class_name.connection if class_name.respond_to?(:connection) with @connection = table_name.classify.constantize.connection
class Fixtures def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE) @connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter @name = table_name # preserve fixture base name @class_name = class_name || (ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize) @table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}" @table_name = class_name.table_name if class_name.respond_to?(:table_name) @connection = table_name.classify.constantize.connection read_fixture_files end end
-Fredrik
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>