[英]belongs_to and references actually create relations in table?
我在Rails 4.2上處於紅寶石狀態。 現在,使用關系並嘗試在.net MVC的ContosoUniversity大學的示例中顯示的講師和Office_Assignments之間建立一對一關系。 請點擊這里查看聯系方式。
在ruby中,我正在遵循本教程。
我使用以下命令添加了教師模型。
rails g model Instructor LastName:string FirstMidName:string HireDate:date
然后像下面一樣創建Office_Assignments
rails g model OfficeAssignments Location:string
現在,我更新了下面的關系模型類。
class Instructor < ActiveRecord::Base
has_one :office_assignment
end
class OfficeAssignment < ActiveRecord::Base
belongs_to :instructor
end
在create_office_assignments遷移文件中,我針對關系進行了如下更改
class CreateOfficeAssignments < ActiveRecord::Migration
def change
create_table :office_assignments do |t|
t.belongs_to :instructor
t.string :Location
t.timestamps null: false
end
end
end
因此,當我使用migrate命令時,創建的表和創建的列如下所示在mysql中
mysql>描述office_assignments;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| instructor_id | int(11) | YES | | NULL | |
| Location | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
但是當我檢查如下所示的關系時,它沒有創建任何外鍵關系。
mysql>從information_schema.key_column_usage中選擇table_name,column_name,constraint_name,referenced_table_name,referenced_column_name,其中constraint_schema ='ContosoUniversity';
+--------------------+-------------+--------------------------+-----------------------+------------------------+
| table_name | column_name | constraint_name | referenced_table_name | referenced_column_name |
+--------------------+-------------+--------------------------+-----------------------+------------------------+
| instructors | id | PRIMARY | NULL | NULL |
| office_assignments | id | PRIMARY | NULL | NULL |
| schema_migrations | version | unique_schema_migrations | NULL | NULL |
| students | id | PRIMARY | NULL | NULL |
+--------------------+-------------+--------------------------+-----------------------+------------------------+
4 rows in set (0.00 sec)
在這里,我很困惑為什么不創建外鍵,或者在遷移過程中遺漏了任何東西? 我也需要將主鍵和外鍵設置為同一列。 我讀了有關add_foriegn_key的內容,但聽不懂
跑
rails g model Instructor LastName:string FirstMidName:string HireDate:date
rails g model OfficeAssignments Location:string instructor:references
將創建數據庫級別約束,在模型中添加belongs_to / has_one,然后檢查生成的遷移文件。 您還可以在其中添加on_update:'delete',依此類推。
默認情況下,Rails不會創建外鍵約束(僅從4.2開始,您就能夠在不使用原始sql或額外的gem的情況下執行此操作)
如果你把
add_foreign_key :office_assignments, :instructors
在遷移中,它將添加約束(如果我的內存正確,也可以與變更表塊一起使用)
在Rails 4.2.1(尚未發布)中,您可以
t.belongs_to :instructor, foreign_key: true
一勞永逸
我像下面以及在模型中一樣更改了遷移,以包含主鍵
class CreateOfficeAssignments < ActiveRecord::Migration
def change
create_table :office_assignments, :id => false do |t|
t.primary_key :Instructor_Id
t.string :Location
t.timestamps null: false
end
add_foreign_key :office_assignments, :instructors, column: :Instructor_Id, primary_key: "Id"
end
end
class OfficeAssignment < ActiveRecord::Base
set_primary_key :Instructor_Id
belongs_to :instructor
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.