2012-03-18 57 views
-3

如何在Rails中添加一个唯一的自动递增主键(IdCustomer)?如果我有一个以上的密钥呢?如何在Rails迁移中添加主键?

class CreateCustomers < ActiveRecord::Migration 
    def change 
    create_table :customers do |t| 
     t.integer :IdCustomer 
     t.string :Login 
     t.string :Password 
     t.string :Email 
     t.string :Skype 
     t.integer :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 
    end 
    end 
end 

请帮我解决这个问题,请把它写在我的代码上。现在我得到

rake aborted! An error has occurred, all later migrations canceled:

Mysql2::Error: Incorrect table definition; there can be only one auto column and it must be defined as a key: CREATE TABLE credit_cards (id int(11) DEFAULT NULL auto_increment PRIMARY KEY, IdCustomer int(11), IdCard int(11) DEFAULT NULL auto_increment PRIMARY KEY, Number varchar(255), NameOfCard int(11), ExpiryDate date, created_at datetime NOT NULL, updated_at datetime NOT NULL) ENGINE=InnoDB

+0

有人知道吗? hm – byCoder 2012-03-22 20:45:46

回答

0

您不能设置多于1列auto_increment。删除auto_increment PRIMARY KEYIdCard int(11) DEFAULT NULL并把UNIQUE而不是如果你想使该列独特

+0

请问我在迁移中如何做到这一点? – byCoder 2012-03-18 18:34:49