我知道我可以触摸迁移并添加
add_index :table_name, :column_name, :unique => true
但是,正确的rails迁移命令是如何生成这些的呢?
rails g migration add_index_to_column_name :column_name, :unique => true
是那么回事吗?
在我的特殊示例中,我有一个表customers
t.integer :customerID
t.string :surname
t.string :first_name
t.string :phone
A我要将customerID设置为唯一。已尝试
rails g migration AddIndexToCustomers :customerID, :unique => true
但如果我在此之后查看我的迁移文件,它看起来并不正确,如下所示:
def change
add_column :customers, :, :customerID,
add_column :customers, :, :unique
add_column :customers, :=, :string
end
有什么想法或建议吗?
发布于 2013-05-09 01:46:58
从Rails 3.2开始,您可以使用:
rails g migration add_index_to_table_name column_name:uniq
来自http://guides.rubyonrails.org/3_2_release_notes.html的示例
rails g scaffold Post title:string:index author:uniq price:decimal{7,2}
upd我很抱歉。如果你不传递的话,默认的类型是string。你可以自己传递类型。
column_name:type:uniq
因此,您的示例应该如下所示:
rails g migration add_index_to_customers customerID:integer:uniq
https://stackoverflow.com/questions/16446814
复制相似问题