开发者

activerecord create_table like existing table

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 sy开发者_高级运维ntax; 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) ?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜