2011-02-01 61 views
1
class CreateTestings < ActiveRecord::Migration 
    def self.up 
    create_table :testings do |t| 
     t.string "name" 
     t.boolean "visible" 
     t.string "description" 
     t.integer "roll" 
     t.references "students" 
     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :testings 
    end 
end 

您好,我剛剛運行此測試遷移以查看Rails如何處理Migrations。即使我已經 t.references "students"Rails遷移未能在MySQL中指定外鍵表

Rails的創建在我testings表students_id成功,但是卻沒有規定它的任何外鍵:

mysql> DESC testings; 
+-------------+--------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+-------------+--------------+------+-----+---------+----------------+ 
| id   | int(11)  | NO | PRI | NULL | auto_increment | 
| name  | varchar(255) | YES |  | NULL |    | 
| visible  | tinyint(1) | YES |  | NULL |    | 
| description | varchar(255) | YES |  | NULL |    | 
| roll  | int(11)  | YES |  | NULL |    | 
| students_id | int(11)  | YES |  | NULL |    | 
| created_at | datetime  | YES |  | NULL |    | 
| updated_at | datetime  | YES |  | NULL |    | 
+-------------+--------------+------+-----+---------+----------------+ 

請問這是怎麼Rails的作品或以其他方式我應該有

t.references :student代替t.references "students"

謝謝!

回答

2

這就是導軌的工作原理。它沒有在其遷移中指定外鍵依賴關係。如果你想指定外鍵關聯,你必須用'execute'命令和SQL代碼手動完成。

+0

我會假設如果Rails的工作方式,這將是很好的做Rails的方式? – 2011-02-01 04:07:36