0

Rails / ActiveRecord 2.3.8でやりたいこと:

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

ただし、ARは、テーブルDDLが複製されているため、フィールドリストが空白であっても、生成されたSQLに「()」を追加することを要求します。その結果、次のようになります。

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

create tableこの単純な新しいlike existingステートメントを生成するようにARを強制する方法はありますか?

明らかにconnection.execute(string)

4

1 に答える 1

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を使用しても問題はありません。簡単なパッチを書きたくない場合は、そうします。

于 2010-08-22T22:54:48.863 に答える