Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using table_name_prefix with hyphen causes MySQL error #3163

Closed
bostrom opened this issue Sep 29, 2011 · 3 comments
Closed

Using table_name_prefix with hyphen causes MySQL error #3163

bostrom opened this issue Sep 29, 2011 · 3 comments

Comments

@bostrom
Copy link

bostrom commented Sep 29, 2011

Using Rails 3.0.10 with a table name prefix that includes a hyphen. This is set up in the model class like

    def self.table_name_prefix
      'my-db.'
    end

which works perfectly fine under certain circumstances. However, when ActiveRecord (or more correctly the MySQL adapter) constructs the sql for a query with, for example, an order by statement, it doesn't quote the prefix with backticks, which makes MySQL choke on the query.

    >> Campaign.last
    ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'my' in 'order clause': SELECT  `my-db`.`campaigns`.* FROM `my-db`.`campaigns` ORDER BY my-db.campaigns.id DESC LIMIT 1

    >> Campaign.inspect
    ActiveRecord::StatementInvalid: Mysql::Error: 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 '-db' at line 1: SHOW TABLES IN my-db

Note the unquoted my-db in the queries.

A possible partial fix for this would be to add

        name = quote_table_name(name) if name
        database = quote_table_name(database) if database

to the tables method of mysql_adapter.rb, like so:

      def tables(name = nil, database = nil) #:nodoc:
        tables = []
        name = quote_table_name(name) if name
        database = quote_table_name(database) if database
        result = execute(["SHOW TABLES", database].compact.join(' IN '), name)
        result.each { |field| tables << field[0] }
        result.free
        tables
      end
@kennyj
Copy link
Contributor

kennyj commented Mar 3, 2012

Hi! @bostrom
I've attemted to fix it. and I think that fixed it. Same to your way :)
Please test it !

kennyj added a commit to kennyj/rails that referenced this issue Mar 4, 2012
@kennyj
Copy link
Contributor

kennyj commented Mar 4, 2012

Anyway, I sent PR :)

@bostrom
Copy link
Author

bostrom commented Mar 4, 2012

Thanks for the fix! But actually I solved it another way (probably the right way). I figured that using another database by prefixing the table name with the database name isn't the right way to go. Instead I defined the other database in config/database.yml and used establish_connection in the model instead.

But this fix might be relevant in other cases also, so a PR is probably not a bad idea anyways.

@drogus drogus closed this as completed Mar 4, 2012
drogus added a commit that referenced this issue Mar 4, 2012
Fix GH #3163. Should quote database on mysql/mysql2.
kennyj added a commit that referenced this issue Jun 11, 2012
Conflicts:

	activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
kennyj added a commit that referenced this issue Jun 11, 2012
Conflicts:

	activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb

Conflicts:

	activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
	activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
kennyj added a commit that referenced this issue Jun 11, 2012
Conflicts:

	activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb

Conflicts:

	activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
	activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb

Conflicts:

	activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
	activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
	activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
	activerecord/test/cases/adapters/mysql2/schema_test.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants