[英]Rails: how to create a migration that add 'using: :btree' to foreign key
[英]Using rails migration generator to add foreign key
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 migration命令来做到这一点?
与其在迁移中手动添加search_operations_id,不如说是:
rails 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.