简体   繁体   English

活动记录关联中的数据库索引

[英]database index in Active Record Associations

let's imagine this situation.I have a article model and a sort model,and they have a one-to-many association. 让我们想象一下这种情况。我有一个article模型和一个sort模型,并且它们具有一对多的关联。

the article migration is : article迁移为:

create_table :articles do |t|
  t.string :title      
  t.string :content
  t.belongs_to :sort

  t.timestamps
end

the sort migration is : sort迁移为:

create_table :sorts do |t|
    t.string :title  

    t.timestamps
end

so the article will be added a filed named sort_id . 因此,将为该article添加一个名为sort_id

now,I want to add an index on the filed sort_id in table articles . 现在,我想在表格articles的文件sort_id上添加索引。

whether I can realize as follow: 我是否可以实现如下:

   add_index :articles, :sort, :unique => true 

or 要么

  add_index :articles, :sort_id, :unique => true 
add_index :articles, :sort_id, :unique => true 

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

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