011*_*112 17 activerecord ruby-on-rails
我正在编写涉及外键的迁移.看着我的同事代码,我看到他添加了这一行: t.reference :tablename, index: true
t.reference部分是有道理的,但我不知道是什么index: true
意思.有人能告诉我吗?我无法在文档中找到它.
注意:这不是重复:Rails ActiveRecord :: Migration index:true和add_index有什么区别?这只是两者的区别,但没有解释他们做了什么.
inf*_*sed 20
index: true
向引用的列添加数据库索引.例如,如果创建:products表:
create_table :products do |t|
t.references :user, index: true
end
Run Code Online (Sandbox Code Playgroud)
这将user_id
在products
表格中创建一个列.它还将在user_id
列上创建一个非唯一索引,名为index_products_on_user_id
.