2013-03-19 63 views
1

我寫了一個連接模型,它看起來像一個遷移:追溯添加主鍵的表

create_table "project_memberships", :id => false, :force => true do |t| 
    t.integer "user_id" 
    t.integer "project_id" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    t.integer "id" 
    end 

我想現在強行創建IDS。我必須刪除表並重新創建它,或者我可以寫一個遷移消除這個約束?

+0

可能重複的[如何將主鍵添加到一個表在軌道](http://stackoverflow.com/questions/9644509/how-to-add-a-primary-key-to-a-table-in-rails) – ABMagil 2014-11-09 15:52:06

+0

感謝您的注意,@ABMagil。我想在這裏留下這個問題,因爲在我的情況下,已經有一個「id」列(鏈接中不是這樣)。不過,如果這對StackOverflow社區沒有太大的區別,我很樂意關閉它。 – 2014-11-09 17:53:48

回答

2

隨着谷歌搜索少量... http://thinkwhere.wordpress.com/2009/05/09/adding-a-primary-key-id-to-table-in-rails/

生成一個空遷移:

rails generate migration AddIdToProjectMemberships 

,並填寫它:

def change 
    add_column :project_memberships, :id, :primary_key 
end  

也有這樣的問題,之前.. how to add a primary key to a table in rails

+0

對不起,我沒有想到在我的搜索中使用術語主鍵。謝謝! – 2013-03-19 22:59:01

+0

沒問題,做了這個工作嗎? – Zippie 2013-03-19 23:06:57

+0

它的確如此,謝謝。 – 2013-03-21 06:29:19