在 Rails 3.2 中添加外键
Adding foreign keys in Rails 3.2
我刚刚继承了一个使用 Rails 3.2 (!!) 的博客平台,我希望添加一些功能。我有两个模型,Article
和 Issue
,我想将它们关联起来,这样任何 Issue
has_many :articles
和任何 Article
belongs_to :issue
.
我已经研究过使用 gem foreigner
来帮助解决这个问题,但我想知道是否有一种方法可以在不添加更多 gem 的情况下做到这一点。我环顾四周,看到了一些相互矛盾的东西。 This SO post 似乎只适用于 Rails 4,我猜它会自动生成这种迁移。
我可以通过迁移在两个模式中创建适当的整数字段,然后将 has_many
和 belong_to
语句放入模型中来手动执行此操作吗?
非常感谢!
编辑:Rails 4
这正是您应该做的:为添加 issue_id 列的文章生成迁移:
$ rails g migration AddIssueToArticle issue:references
这将生成如下所示的迁移,它将添加 issue_id 列并创建索引:
class AddIssueToArticle < ActiveRecord::Migration
def change
add_reference :articles, :issue, index: true
end
end
Rails 3.2
$ rails g migration AddIssueIdToArticle issue_id:integer
编辑生成的迁移并添加索引:
class AddIssueIdToArticle < ActiveRecord::Migration
def change
add_column :articles, :issue_id, :integer
add_index :articles, :issue_id
end
end
两个版本:
运行迁移:
$ rake db:migrate
$ rake db:test:prepare
然后将关系添加到 Article 和 Issue 类:
# ./app/models/article.rb
class Article
belongs_to :issue
# ... the rest of the class
end
# ./app/models/issue.rb
class Issue
has_many :articles
# ... the rest of the class
end
如果您的 类 是命名空间或整数列是不同的名称,那么您需要向 belongs_to
和 has_many
方法参数添加更多数据。
有关生成迁移的更多信息,请参阅 the docs for 4.0 (and 3.2)。
这是我在 Rails 3.2
中添加多个外键的代码片段
class ChangeAdminUsersEmployee < ActiveRecord::Migration
def up
ActiveRecord::Base.transaction do
tables = %i[admin_users]
reference = 'employee'
tables.each do |table|
p sql = <<-SQL
ALTER TABLE `#{table}`
ADD CONSTRAINT `#{table}_#{reference}_fk`
FOREIGN KEY (`#{reference}_id`) REFERENCES `#{reference}s`(`id`);
SQL
p ActiveRecord::Base.connection.execute(sql)
puts "===="
end
end
end
def down
raise NotImplementedError
end
end
我刚刚继承了一个使用 Rails 3.2 (!!) 的博客平台,我希望添加一些功能。我有两个模型,Article
和 Issue
,我想将它们关联起来,这样任何 Issue
has_many :articles
和任何 Article
belongs_to :issue
.
我已经研究过使用 gem foreigner
来帮助解决这个问题,但我想知道是否有一种方法可以在不添加更多 gem 的情况下做到这一点。我环顾四周,看到了一些相互矛盾的东西。 This SO post 似乎只适用于 Rails 4,我猜它会自动生成这种迁移。
我可以通过迁移在两个模式中创建适当的整数字段,然后将 has_many
和 belong_to
语句放入模型中来手动执行此操作吗?
非常感谢!
编辑:Rails 4
这正是您应该做的:为添加 issue_id 列的文章生成迁移:
$ rails g migration AddIssueToArticle issue:references
这将生成如下所示的迁移,它将添加 issue_id 列并创建索引:
class AddIssueToArticle < ActiveRecord::Migration
def change
add_reference :articles, :issue, index: true
end
end
Rails 3.2
$ rails g migration AddIssueIdToArticle issue_id:integer
编辑生成的迁移并添加索引:
class AddIssueIdToArticle < ActiveRecord::Migration
def change
add_column :articles, :issue_id, :integer
add_index :articles, :issue_id
end
end
两个版本:
运行迁移:
$ rake db:migrate
$ rake db:test:prepare
然后将关系添加到 Article 和 Issue 类:
# ./app/models/article.rb
class Article
belongs_to :issue
# ... the rest of the class
end
# ./app/models/issue.rb
class Issue
has_many :articles
# ... the rest of the class
end
如果您的 类 是命名空间或整数列是不同的名称,那么您需要向 belongs_to
和 has_many
方法参数添加更多数据。
有关生成迁移的更多信息,请参阅 the docs for 4.0 (and 3.2)。
这是我在 Rails 3.2
中添加多个外键的代码片段class ChangeAdminUsersEmployee < ActiveRecord::Migration
def up
ActiveRecord::Base.transaction do
tables = %i[admin_users]
reference = 'employee'
tables.each do |table|
p sql = <<-SQL
ALTER TABLE `#{table}`
ADD CONSTRAINT `#{table}_#{reference}_fk`
FOREIGN KEY (`#{reference}_id`) REFERENCES `#{reference}s`(`id`);
SQL
p ActiveRecord::Base.connection.execute(sql)
puts "===="
end
end
end
def down
raise NotImplementedError
end
end