[英]ActiveRecord, what does “index: true” mean?
I'm writing a migration that involves a foreign key.我正在编写一个涉及外键的迁移。 Looking at my colleagues code, I see that he has added the line:
t.reference :tablename, index: true
查看我同事的代码,我看到他添加了这一行:
t.reference :tablename, index: true
The t.reference part makes sense, but I don't know what index: true
means. t.reference 部分是有道理的,但我不知道
index: true
是什么意思。 Can anyone tell me?谁能告诉我? I haven't been able to find that in the docs.
我一直无法在文档中找到它。
Note: This is not a duplicate of: Rails ActiveRecord::Migration what is the difference between index: true and add_index?注意:这不是以下内容的重复: Rails ActiveRecord::Migration index: true 和 add_index 之间有什么区别? Which only diffs the two, but doesn't explain what they do.
这只区分了两者,但没有解释它们的作用。
index: true
adds a database index to the referenced column. index: true
将数据库索引添加到引用的列。 For example, if creating a :products table:例如,如果创建一个 :products 表:
create_table :products do |t|
t.references :user, index: true
end
That will create a non-unique index on the user_id
column in the products
table named index_products_on_user_id
.这将在名为
index_products_on_user_id
的products
表中的user_id
列上创建一个非唯一索引。
well, when you create this add_reference thing you are saying to rails to add a user_id within "product table". 好吧,当你创建这个add_reference的东西时,你要告诉rails在“产品表”中添加user_id。 i think the id's are useful to connect differents tables.
我认为id对连接不同的表很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.