如何在Rails迁移中添加不同表名的外键

27

如何在添加外键时分配不同的表名。例如

我有一个模型:

class MyPost < ActiveRecord::Base
  has_many :comments, class_name: PostComment
end

class PostComment < ActiveRecord::Base
  belongs_to :post, class_name: MyPost
end

现在我想将我的迁移文件更改为以下内容:
class CreatePostComments < ActiveRecord::Migration
  def change
    create_table :post_comments do |t|
     t.belongs_to :post, index: true
     t.timestamps null: false
    end
    add_foreign_key :post, :class_name => MyPost
  end
end 

但是它没有起作用。迁移正在被取消。我该如何更改我的迁移文件以适应我的模型结构。


1
t.belongs_to :post, index: true 为你创建了外键。为什么你还要尝试呢?我不明白你在尝试做什么。 - Arup Rakshit
你的迁移生成了什么错误? - nayiaw
2个回答

48
您可以按照以下方式传递外键选项:
class CreatePostComments < ActiveRecord::Migration
  def change
    create_table :post_comments do |t|
      t.references :post, foreign_key: { to_table: :my_posts }, index: true
      t.timestamps null: false
    end
  end
end

如果您想添加唯一约束条件,对于索引选项也是如此:

t.references :post, foreign_key: { to_table: :my_posts }, index: { unique: true}

顺便提一下,referencesbelongs_to的别名,更确切地说,belongs_toreferences的别名。

详细信息请参见实现rails 5.0.rc2rails 4.2


7
默认情况下,indextrueindex: true 是不必要的。 - developer033
8
Rails 5 默认情况下 indextrue。在之前的 Rails 版本中,默认值为 false。原文链接index is true by default from Rails 5. index is falseby default on previous Rails versions - cbliard
1
我会将其修改为t.references :post, foreign_key: { to_table: :my_posts }, null:false,因为默认情况下它不会添加null: false - Alexander Gorg

18

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接