2011-01-11 46 views
0

我試圖建立一個HABTM關係的遷移文件,但是當我跑,我發現了以下錯誤遷移:的Rails 3:遷移HABTM,主鍵的問題

Primary key is not allowed in a has_and_belongs_to_many join table (parts_vehicles).

這裏是我的遷移文件(20110111035950_create_parts_vehicles.rb):

class CreatePartsVehiclesJoinTable < ActiveRecord::Migration 
    def self.up 
    create_table :parts_vehicles, :id => false do |t| 
     t.integer :part_id 
     t.integer :vehicle_id 
    end 
    end 

    def self.down 
    drop_table :parts_vehicles 
    end 
end 

The documentation實例狀態中使用:id => false從正在生成禁用主鍵,但我仍然得到錯誤。

+0

它看起來是正確的。運行`rake db:migrate:status`來確保這個遷移是'down'。 – 2011-01-11 05:02:26

+0

是的,這是正確的。它在我放棄我的數據庫並重新運行遷移時開始工作。 – 2011-01-11 21:23:05

回答

1

1)你的類名應該是相同的遷移名稱:

class CreatePartsVehicles < ActiveRecord::Migration 

2)你有沒有遷移?嘗試刪除你的數據庫(耙分貝:下降)和remigrating(耙分貝:遷移)