0

I want create foreign keys, but i fails to create them (foreign keys for teacher_id, course_id) .

Please see the code - what should i change to produce foreign keys?

The case app = school

Steps:

console:

rails new school
rails g model teacher name:string
rails g model course name:string
rails g model teachercourse teacher_id:integer course_id:integer

add code to models:

class Course < ActiveRecord::Base
  attr_accessible :name
  has_many :teachers, through: :teachercourse
end

class Teacher < ActiveRecord::Base
  attr_accessible :name
  has_many :courses, through: :teachercourse
end

class Teachercourse < ActiveRecord::Base
  attr_accessible :course_id, :teacher_id
  belongs_to :course
  belongs_to :teacher
end

add code to migrations:

class CreateTeachercourses < ActiveRecord::Migration
  def change
    create_table :teachercourses do |t|
      t.integer :teacher_id
      t.integer :course_id

      t.timestamps
    end
    add_index :teacher_id
    add_index :course_id
  end
end

console:

rake db:migrate
rake db:schema:load

mysql db innodb partial dump(no foreign keys for teacher_id, course_id):

CREATE TABLE IF NOT EXISTS `courses` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `teachercourses` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `teacher_id` int(11) DEFAULT NULL,
  `course_id` int(11) DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `index_teachercourses_on_course_id` (`course_id`),
  KEY `index_teachercourses_on_teacher_id` (`teacher_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `teachers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
Ben
  • 25,389
  • 34
  • 109
  • 165
  • i try create foreign keys without success (foreign keys for teacher_id, course_id) - i use in the code : add_index + has_many + belong_to. its create KEY and not foreign_key – Ben Aug 14 '12 at 10:43
  • Ok, so you must mean the constraint, no the actual columns. In that case, look at @Sandip's answer – davids Aug 14 '12 at 11:05

2 Answers2

3

Just suggesting a few changes to improve your code, but @Sandip's answer is correct as per this SO question: Why do Rails migrations define foreign keys in the application but not in the database?

Models

class Course < ActiveRecord::Base
  attr_accessible :name
  has_many :teachercourses
  has_many :teachers, through: :teachercourses
end

class Teacher < ActiveRecord::Base
  attr_accessible :name
  has_many :teachercourses
  has_many :courses, through: :teachercourses
end

class Teachercourse < ActiveRecord::Base
  attr_accessible :course_id, :teacher_id
  belongs_to :course
  belongs_to :teacher
end

Migrations

class CreateTeachercourses < ActiveRecord::Migration
  def change
    create_table :teachercourses do |t|
      t.integer :teacher_id
      t.integer :course_id

      t.timestamps
    end
    add_index :teachercourses, :teacher_id
    add_index :teachercourses, :course_id
  end
end
Community
  • 1
  • 1
Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
2

By default rails do not generate foreign keys for any association. If you think you should have foreign_keys inside DB then you need to manually add those.

Sandip Ransing
  • 7,583
  • 4
  • 37
  • 48