2014-10-09 27 views
1

我已經叫學生表需要比ID其他在軌主鍵的活動記錄遷移腳本

mysql> desc students; 
+--------------------+---------------+------+-----+---------+----------------+ 
| Field    | Type   | Null | Key | Default | Extra   | 
+--------------------+---------------+------+-----+---------+----------------+ 
| id     | int(11)  | NO | PRI | NULL | auto_increment | 
| height    | decimal(15,8) | YES |  | NULL |    | 
| name    | varchar(255) | YES |  | NULL |    | 
| text    | varchar(255) | YES |  | NULL |    | 
| status    | tinyint(4) | YES |  | NULL |    | 
| created_at   | datetime  | YES |  | NULL |    | 
| updated_at   | datetime  | YES |  | NULL |    | 
+--------------------+---------------+------+-----+---------+----------------+ 

我想創建一個名爲students_data新表具有student_name爲外鍵和名稱作爲主鍵從學生桌上。我應該如何改變我的腳本?以下腳本以學生表中的主鍵作爲ID

class CreateStudentsData < ActiveRecord::Migration 
    def change 
    create_table :students_data do |t| 
     t.belongs_to :students, :foreign_key => :student_name, :primary_key => :name 
    end 
    end 
end 

如何更改我的腳本以獲取名稱作爲主鍵?

注意:我不想將primary_key標識設置爲false,建議here。我需要id和name作爲主鍵。

+0

只是爲了興趣:你爲什麼需要它?爲什麼不在student_data表中使用'student_id'? – IS04 2014-10-09 09:38:43

+0

@ IS04 - 它是我的經理要求 – Sam 2014-10-09 09:43:30

回答