web-dev-qa-db-ja.com

rails 3.2移行では、changeメソッドでcreate_tableにインデックスを追加できません

Rails 3.2.2:

class CreateStatistics < ActiveRecord::Migration
  def change
    create_table :statistics do |t|
      t.string :name
      t.integer :item_id
      t.integer :value
      t.text :desc

      t.timestamps
      t.index [:name, :item_id]
    end

  end
end

そしてここに移行エラーがあります:

==  CreateStatistics: migrating ===============================================
-- create_table(:statistics)
ActiveRecord::ConnectionAdapters::TableDefinition
rake aborted!
An error has occurred, all later migrations canceled:

undefined method `index' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0xbd16888>

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

インデックスを作成する正しい方法は何ですか?

37
linjunhalida

「変更」移行の一部としてインデックスを追加することもできます。 create_tableの呼び出し以外で行う必要があります。

class CreateStatistics < ActiveRecord::Migration
  def change
    create_table :statistics do |t|
      t.string :name
      t.integer :item_id
      t.integer :value
      t.text :desc

      t.timestamps
    end

    add_index :statistics, [:name, :item_id]
  end
end

これにより、「アップ」マイグレーションでテーブルとインデックスが正しく作成され、「ダウン」マイグレーションでインデックスとテーブルが削除されます。

73
Brandan

だから私はそれを古い方法に変えればうまくいきます。変更方法を使用してこれを行う新しい方法があると思います。

class CreateStatistics < ActiveRecord::Migration
  def up
    create_table :statistics do |t|
      t.string :name
      t.integer :item_id
      t.integer :value
      t.text :desc

      t.timestamps
    end
    add_index :statistics, [:name, :item_id]
  end

  def down
    drop_table :statistics
  end
end
4
linjunhalida

複数のインデックスがあり、個々のadd_index呼び出しでテーブル名を何度も繰り返したくない場合は、create_tableに続くchange_tableブロックを使用できます。

create_table :user_states do |t|
  t.references :user, :null => false
  t.integer :rank
  t.integer :status_code
end

change_table :user_states do |t|
  t.index [:rank, :status_code]
end
3
user1792037
    class CreateTempPfp < ActiveRecord::Migration
      def change
        create_table :temp_ptps do |t|
          t.string :owner
          t.integer :source_id
          t.string :source_type
          t.integer :year
          t.string :pcb_type
          t.float :january
          t.float :february
          t.float :march
          t.float :april
          t.float :may
          t.float :june
          t.float :july
          t.float :august
          t.float :september
          t.float :october
          t.float :november
          t.float :december
          t.float :dollar_per_sqft
          t.float :dollar_per_unit
          t.integer :rp_acc_code
          t.integer :rp_property_id
          t.integer :real_estate_property_id
          t.timestamps
       end
       add_index :temp_ptps, [:source_id, :source_type]
     end
   end
2
user3040683

create_tableActiveRecord::ConnectionAdapters::TableDefinitionクラスを生成するように見えます。このクラスにはメソッドindexが含まれていません。代わりに、change_tableは、このindexメソッドを含むActiveRecord::ConnectionAdapters::Tableクラスを生成するように見えます。

Create_tableの移行中にインデックスを追加する場合は、次のことを試してください。

class CreateStatistics < ActiveRecord::Migration
  def self.up
    create_table :statistics do |t|
      t.string :name
      t.integer :item_id
      t.integer :value
      t.text :desc

      t.timestamps
    end

    add_index :statistics, :name
    add_index :statistics, :item_id
  end

  def self.down
    drop_table :statistics
  end
end
1
Lee Jarvis