2012-03-15 11 views
36

qui è la mia migrazione in Rails 3.2.2:rotaie 3.2 migrazione non può aggiungere indice per CREATE_TABLE nel metodo di cambiamento

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 

e qui è l'errore migrazione:

== 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) 

qual è il modo giusto creare un indice?

risposta

72

È ancora possibile aggiungere un indice come parte di una migrazione di "modifica". Non vi resta che farlo al di fuori della chiamata alla 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 

Questo crea correttamente la tavola e poi l'indice su una migrazione "up" e gocce l'indice e poi il tavolo su una migrazione "verso il basso".

+1

Una nota veloce: @ La risposta di Brandan qui è" più retta "di injeckt's per Rails 3 migrazioni di stile che consentono i metodi 'change' invece dei metodi' up' e 'down' vecchio stile. Entrambi vanno bene, mi ci è voluto un minuto per capire la differenza. –

1

Sembra che create_table restituisca una classe ActiveRecord::ConnectionAdapters::TableDefinition. Questa classe non contiene il metodo index. Invece, change_table sembra produrre una classe ActiveRecord::ConnectionAdapters::Table che include questo metodo index.

Se si desidera aggiungere un indice durante una migrazione CREATE_TABLE, provate questo:

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 
+0

sì, e l'attuale modo di creare tabelle e indici? – linjunhalida

+0

usa 'add_index: table_name, ...' al di fuori della tua chiamata 'create_table' –

+0

aggiorna la mia risposta –

4

quindi lo cambio alla vecchia maniera, e funziona. e penso che ci sia un nuovo modo di farlo usando il metodo di cambiamento.

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 
+0

è esattamente quello che ho detto nella mia risposta –

3

Se si dispone di più di un indice e non si desidera ripetere più volte il nome della tabella nelle singole chiamate add_index, è possibile utilizzare un blocco change_table che segue la tabella create_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 
2
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 
Problemi correlati