2012-03-15 30 views
36

đây là di cư của tôi ở đường ray 3.2.2:ray 3.2 di cư không thể thêm chỉ số để create_table trong phương pháp thay đổi

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 

và đây là lỗi di chuyển:

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

đúng cách là những gì để tạo chỉ mục?

Trả lời

72

Bạn vẫn có thể thêm chỉ mục làm một phần của quá trình di chuyển "thay đổi". Bạn chỉ cần có để làm điều đó bên ngoài của các cuộc gọi đến 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 

này một cách chính xác tạo ra bảng và sau đó chỉ số trên một "lên" di cư và giảm chỉ số và sau đó bàn về một sự chuyển đổi "xuống".

+1

Lưu ý nhanh: @ Câu trả lời của Brandan ở đây là" righter "hơn injeckt's for the Rails 3 di chuyển kiểu cho phép 'thay đổi' phương thức thay vì phương thức' up' và 'down' kiểu cũ. Cả hai đều tốt, nó chỉ mất một phút để nhận ra sự khác biệt. –

1

Có vẻ như create_table mang lại một lớp học ActiveRecord::ConnectionAdapters::TableDefinition. Lớp này không chứa phương thức index. Thay vào đó, change_table dường như mang lại một lớp học ActiveRecord::ConnectionAdapters::Table bao gồm phương pháp index này.

Nếu bạn muốn thêm một chỉ số trong một di chuyển create_table, hãy thử này:

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

có, và cách tạo bảng và chỉ mục hiện tại? – linjunhalida

+0

sử dụng 'add_index: table_name, ...' bên ngoài lệnh 'create_table' của bạn –

+0

cập nhật câu trả lời của tôi –

4

vì vậy tôi thay đổi nó lại con đường cũ, và nó hoạt động. và tôi nghĩ có một cách mới để thực hiện điều này bằng cách sử dụng phương pháp thay đổi.

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

đó chính xác là những gì tôi đã nói trong câu trả lời của tôi –

3

Nếu bạn có nhiều chỉ mục và không muốn lặp lại tên bảng nhiều lần trong các lệnh gọi add_index riêng lẻ, bạn có thể sử dụng khối có thể thay đổi theo sau chữ 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 
Các vấn đề liên quan