外键不是在很多情况下创建的rails3 - 创建“密钥”而不是“外键”

时间:2012-08-14 10:29:20

标签: ruby-on-rails ruby-on-rails-3 database-design foreign-keys data-modeling

我想要创建外键, 但是我没有创建它们(teacher_id的外键,course_id)。

请参阅代码 - 我应该更改什么才能生成外键?

案例app =学校

步骤:

控制台:

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

向模型添加代码:

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

向迁移添加代码:

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

控制台:

rake db:migrate
rake db:schema:load

mysql db innodb部分转储(没有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 ;

2 个答案:

答案 0 :(得分:3)

只是建议进行一些更改以改进您的代码,但@Sandip的答案是正确的,因为这个问题:Why do Rails migrations define foreign keys in the application but not in the database?

模型

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

迁移

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

答案 1 :(得分:2)

默认情况下,rails不会为任何关联生成外键。如果您认为在DB中应该有foreign_keys,那么您需要手动添加它们。