我正在写一个涉及外键的迁移 . 看着我的同事代码,我看到他添加了这一行: t.reference :tablename, index: true
t.reference部分是有道理的,但我不知道 index: true
的意思 . 有人能告诉我吗?我无法在文档中找到它 .
注意:这不是重复:Rails ActiveRecord::Migration what is the difference between index: true and add_index?这只是两者的区别,但不解释它们的作用 .
2 回答
index: true
将数据库索引添加到引用的列 . 例如,如果创建:products表:这将在
products
表中创建user_id
列 . 它还将在user_id
列上创建一个名为index_products_on_user_id
的非唯一索引 .好吧,当你创建这个add_reference的东西时,你要告诉rails在“产品表”中添加user_id . 我认为id对连接不同的表很有用 .