[英]belongs_to and references actually create relations in table?
I am in ruby on rails 4.2. 我在Rails 4.2上处于红宝石状态。 Now working with relation and try to create one-one relation ship between Instructor and Office_Assignments shown in the ContosoUniversity of .net MVC sample. 现在,使用关系并尝试在.net MVC的ContosoUniversity大学的示例中显示的讲师和Office_Assignments之间建立一对一关系。 Please click here to see the relation details. 请点击这里查看联系方式。
In ruby i am following this tutorial. 在ruby中,我正在遵循本教程。
I added Instructor model using the following command. 我使用以下命令添加了教师模型。
rails g model Instructor LastName:string FirstMidName:string HireDate:date
and then create Office_Assignments like below 然后像下面一样创建Office_Assignments
rails g model OfficeAssignments Location:string
Now i updated the model classes like below for relations. 现在,我更新了下面的关系模型类。
class Instructor < ActiveRecord::Base
has_one :office_assignment
end
class OfficeAssignment < ActiveRecord::Base
belongs_to :instructor
end
In the create_office_assignments migration file i changed like below for relations 在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
So when i use migrate command, tables created and columns created like below in mysql 因此,当我使用migrate命令时,创建的表和创建的列如下所示在mysql中
mysql>describe office_assignments; 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)
but when i check the relationship like below, it didn't create any foreign key relation ship. 但是当我检查如下所示的关系时,它没有创建任何外键关系。
mysql> select table_name, column_name, constraint_name, referenced_table_name, referenced_column_name from information_schema.key_column_usage where constraint_schema = 'ContosoUniversity'; 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)
Here i am confused why not create foreign key or i missed anything in my migration or some where? 在这里,我很困惑为什么不创建外键,或者在迁移过程中遗漏了任何东西? also i need to set the primary key and foreign key as same column. 我也需要将主键和外键设置为同一列。 I read about add_foriegn_key, but didn't understand 我读了有关add_foriegn_key的内容,但听不懂
run 跑
rails g model Instructor LastName:string FirstMidName:string HireDate:date
rails g model OfficeAssignments Location:string instructor:references
will create DB level constraints, add belongs_to/has_one to models, than check generated migration file. 将创建数据库级别约束,在模型中添加belongs_to / has_one,然后检查生成的迁移文件。 There you can also add on_update: 'delete' and so on. 您还可以在其中添加on_update:'delete',依此类推。
Rails doesn't create foreign key constraints by default (it's only since 4.2 that you've been able to do this without using raw sql or an extra gem) 默认情况下,Rails不会创建外键约束(仅从4.2开始,您就能够在不使用原始sql或额外的gem的情况下执行此操作)
If you put 如果你把
add_foreign_key :office_assignments, :instructors
In a migration then it will add the constraint (there's also a variant you can use with a change table block if my memory is correct) 在迁移中,它将添加约束(如果我的内存正确,也可以与变更表块一起使用)
In rails 4.2.1 (not yet released) you'll be able to do 在Rails 4.2.1(尚未发布)中,您可以
t.belongs_to :instructor, foreign_key: true
To do it all in one go 一劳永逸
I changed my migration like below and also in model to include my primary key 我像下面以及在模型中一样更改了迁移,以包含主键
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.