简体   繁体   中英

belongs_to and references actually create relations in table?

I am in ruby on 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. Please click here to see the relation details.

In ruby i am following this tutorial.

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

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

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

mysql>describe 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';

+--------------------+-------------+--------------------------+-----------------------+------------------------+
| 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

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. There you can also add on_update: 'delete' and so on.

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM