rails 4.2.4
ruby 2.2.1
要自动生成国家/地区迁移,我会这样做:
rails g migration CreateCountry abbreviation:string status:string search_operations_id:integer
我想向生成的迁移文件添加外键:
add_foreign_key :countries, :search_operations, on_delete: :cascade
如何使用 Rails g 迁移命令来做到这一点?
最佳答案
您实际上可以直接说:
,而不是在迁移中手动添加 search_operations_idrails g migration CreateCountry ... search_operations:references
当 Rails 生成迁移时,您应该看到如下内容:
class CreateCountry < ActiveRecord::Migration
def change
...
add_reference :countries, :search_operations, index: true
add_foreign_key :countries, :search_operations
end
end
关于ruby-on-rails - 使用rails迁移生成器添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33073089/