Search code examples
mysqlruby-on-railsruby-on-rails-4foreign-key-relationship

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


Solution

  • 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.