繁体   English   中英

ActiveRecord,“index: true”是什么意思?

[英]ActiveRecord, what does “index: true” mean?

我正在编写一个涉及外键的迁移。 查看我同事的代码,我看到他添加了这一行: t.reference :tablename, index: true

t.reference 部分是有道理的,但我不知道index: true是什么意思。 谁能告诉我? 我一直无法在文档中找到它。

注意:这不是以下内容的重复: Rails ActiveRecord::Migration index: true 和 add_index 之间有什么区别? 这只区分了两者,但没有解释它们的作用。

index: true将数据库索引添加到引用的列。 例如,如果创建一个 :products 表:

create_table :products do |t|
  t.references :user, index: true
end

这将在名为index_products_on_user_idproducts表中的user_id列上创建一个非唯一索引。

好吧,当你创建这个add_reference的东西时,你要告诉rails在“产品表”中添加user_id。 我认为id对连接不同的表很有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM