2012-04-09 36 views
5

如何为我的IdClient字段设置主键?我试过所有的方法,但我会得到错误(rails 3.0.9)...你能帮我吗?如何在Rails迁移中设置ruby中的主键?

class CreateCustomers < ActiveRecord::Migration 
    def self.up 
    create_table :customers do |t| 
     t.integer :IdCustomer 
     t.string :username 
     t.string :crypted_password 
     t.string :password_salt 
     t.string :persistence_token 
     t.string :email 
     t.string :Skype 
     t.string :ICQ 
     t.string :Firstname 
     t.string :Lastname 
     t.string :Country 
     t.string :State 
     t.string :City 
     t.string :Street 
     t.string :Building 
     t.integer :Room 
     t.string :AddressNote 
     t.date :DateOfReg 
     t.integer :CustGroup 
     t.float :TotalBuy 

     t.timestamps 
     add_index(:customers, :IdCustomer, :unique => true) 
    end 
    end 

    def self.down 
    drop_table :customers 
    end 
end 

另外如何在模型中设置关系?

回答

16

不要这样做。使用内置的id字段作为主键。如果你打算使用Rails,你应该用“Rails方式”构建应用程序,除非你有很好的理由不要。

如果你真的想这样做,你可以传递一个:primary_key选项create_table

create_table :customers, :primary_key => :idClient do |t| 
    # ... 
end 

您还需要通过告诉你的模型的主键的名称self.primary_key = "idClient"

+0

但为什么没有?所以我不再需要我的领域idclient? – byCoder 2012-04-10 06:26:57

+0

@ ovatsug25你看完我的回答了吗?这将包括我提到的“*非常好的理由*”。 – meagar 2013-01-25 15:22:12

+0

ohhh,我知道...谢谢你指出技术......但我仍然不想这样做...... – ovatsug25 2013-01-25 16:27:48