如何通过Rails迁移创建新表,并为其添加唯一索引?
在文档中,我发现了创建表后如何向表中添加索引,但是如何在同一迁移文件中创建表并添加唯一索引呢?
这是完整的过程:
生成迁移(rails generate migration CreateFoos bar:string
或rails g migration CreateFoos bar:string
)
修改您的迁移,使其看起来像这样:
class CreateFoos < ActiveRecord::Migration
def change
create_table :foos do |t|
t.string :bar, :null => false
t.index :bar, unique: true
end
end
end
跑 rake db:migrate
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句