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. Can anyone tell me? I haven't been able to find that in the docs.
t.reference部分是有道理的,但我不知道索引:true表示。有人能告诉我吗?我无法在文档中找到它。
Note: This is not a duplicate of: Rails ActiveRecord::Migration what is the difference between index: true and add_index? Which only diffs the two, but doesn't explain what they do.
注意:这不是重复:Rails ActiveRecord :: Migration index:true和add_index有什么区别?这只是两者的区别,但没有解释他们做了什么。
2 个解决方案
#1
16
index: true
adds a database index to the referenced column. For example, if creating a :products table:
index:true将数据库索引添加到引用的列。例如,如果创建:products表:
create_table :products do |t|
t.references :user, index: true
end
That will create a user_id
column in the products
table. It will also create a non-unique index on the user_id
column, named index_products_on_user_id
.
这将在products表中创建user_id列。它还将在user_id列上创建一个名为index_products_on_user_id的非唯一索引。
#2
0
well, when you create this add_reference thing you are saying to rails to add a user_id within "product table". i think the id's are useful to connect differents tables.
好吧,当你创建这个add_reference的东西时,你要告诉rails在“产品表”中添加一个user_id。我认为id对连接不同的表很有用。
#1
16
index: true
adds a database index to the referenced column. For example, if creating a :products table:
index:true将数据库索引添加到引用的列。例如,如果创建:products表:
create_table :products do |t|
t.references :user, index: true
end
That will create a user_id
column in the products
table. It will also create a non-unique index on the user_id
column, named index_products_on_user_id
.
这将在products表中创建user_id列。它还将在user_id列上创建一个名为index_products_on_user_id的非唯一索引。
#2
0
well, when you create this add_reference thing you are saying to rails to add a user_id within "product table". i think the id's are useful to connect differents tables.
好吧,当你创建这个add_reference的东西时,你要告诉rails在“产品表”中添加一个user_id。我认为id对连接不同的表很有用。