添加索引:通过生成迁移在rails上的ruby中的列是唯一的 [英] Adding index :unique to a column in ruby on rails via generate migration
问题描述
我知道我可以触摸迁移并添加
I know that i can touch a migration and add
add_index :table_name, :column_name, :unique => true
但是,如何使用right rails migration命令生成这个命令?
But how is the right rails migration command to generate this?
rails g migration add_index_to_column_name :column_name, :unique => true
是吧?
我的特殊例子我有一个表customers
In my special example I have a table customers
t.integer :customerID
t.string :surname
t.string :first_name
t.string :phone
到唯一。尝试
rails g migration AddIndexToCustomers :customerID, :unique => true
但是如果我看到我的迁移文件之后,它看起来不正确看到这: p>
But if i look to my migration file after this, it dont look right see this:
def change
add_column :customers, :, :customerID,
add_column :customers, :, :unique
add_column :customers, :=, :string
end
任何想法或建议?
推荐答案
从Rails 3.2开始,您可以使用:
Starting from Rails 3.2 you able to use:
rails g migration add_index_to_table_name column_name:uniq
=http://guides.rubyonrails.org/3_2_release_notes.html> http://guides.rubyonrails.org/3_2_release_notes.html
example from http://guides.rubyonrails.org/3_2_release_notes.html
rails g scaffold Post title:string:index author:uniq price:decimal{7,2}
upd 很抱歉。默认类型,如果你不传递它将是字符串。您可以自行传递类型。
upd I'm sorry. The default type if you don't pass it would be string. You can pass type by yourself.
column_name:type:uniq
因此你的例子应该是:
rails g migration add_index_to_customers customerID:integer:uniq
这篇关于添加索引:通过生成迁移在rails上的ruby中的列是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!