2013-03-27 71 views
-4

在使用rake在我的pgslq數據庫上創建表時,rails allways創建一個「id」字段作爲主鍵。Rails(rake)默認ID字段

我的問題是,我需要有一個自定義名稱的ID字段。我如何才能指定數據庫字段只有我想要的字段,指定主鍵?

+4

這是覆蓋在[多](http://stackoverflow.com/questions/9986658/setting-primary-key -for-a-database-not-named-id)[other](http://stackoverflow.com/questions/1200568/using-rails-how-can-i-set-my-primary-key-to-not -be-an-integer-typed-column)堆棧溢出問題和[all the web](http://roninonrails.blogspot.com/2008/06/using-non-standard-primary-keys-with.html) ,但我只是補充說它會讓你的生活變得糟糕,你應該盡一切力量在可能的情況下切換到表中使用'id'。這是Rails的主要缺點,但'id'使事情變得簡單。 – 2013-03-27 13:24:12

+0

Downvote for not so so before posting a redundant question。 – 2013-03-27 13:25:27

+0

我做了一個快速搜索,並沒有發現任何東西。感謝關於不使用帶缺省名稱的ID的問題的額外信息...問題是因爲我們有數據模型,現在我們正在討論它。 – Techmago 2013-03-27 14:00:37

回答

3

可以指定主鍵遷移傳遞它的名字create_table

create_table :my_table, :primary_key => :custom_id do |t| 
    #... 
end