Question

With Rails/ActiveRecord 2.3.8 I'd like to do:

AnyModel.connection.create_table( 'temp_any_model', temporary: true, id: false, options: 'like any_model' )

But AR insists on adding "()" to the generated SQL even though the field list is blank since the table DDL is being cloned, thus resulting in e.g.:

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 ') like any_model' at line 1: 
CREATE TEMPORARY TABLE `temp_any_model` () like any_model

Is there any way to coerce AR to generate this simple create tablenewlike existing statement?

Besides obviously connection.execute(string) ?

Was it helpful?

Solution

Nope. The parenthesis are hard coded in create_table:

def create_table(table_name, options = {})
  # snipped ...

  create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "            
  create_sql << "#{quote_table_name(table_name)} ("                             
  create_sql << table_definition.to_sql                                         
  create_sql << ") #{options[:options]}"                                        
  execute create_sql        
end

There's nothing wrong with using execute on a string literal; I would do that if you don't feel like writing a quick patch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top