簡體   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