使用Rails / ActiveRecord 2.3.8我想做:
AnyModel.connection.create_table( 'temp_any_model', temporary: true, id: false, options: 'like any_model' )
但是AR坚持在生成的SQL中添加“()”,即使字段列表为空,因为表DDL正在被克隆,因此导致例如:
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
有没有办法强制AR生成这个简单的create table
新like existing
声明?
除了显然connection.execute(string)
?
答案 0 :(得分:2)
不。括号在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
对字符串文字使用execute没有任何问题;如果你不想写一个快速补丁,我会这样做。