在Rails的多列索引中获取[nil,nil]

时间:2018-09-05 01:19:13

标签: ruby-on-rails ruby activerecord ruby-on-rails-5

我有这个:

class CreateStudentHasSubjects < ActiveRecord::Migration[5.2]                                                                                                            
  def change                                                                                                                                                             
    create_table :student_has_subjects do |t|                                                                                                                            
      t.references :student, null: false, foreign_key: true                                                                                                              
      t.references :subject, null: false, foreign_key: true                                                                                                              
      t.boolean :is_active, null: false, default: true                                                                                                                   

      t.index [:student, :subject] #Here's where the question comes in.                                                                                                                                       

      t.timestamps                                                                                                                                                       
    end                                                                                                                                                                  
  end                                                                                                                                                                    
end        

执行$ rails db:migrate后,我得到schema.rb文件:

create_table "student_has_subjects", force: :cascade do |t|                                                                                                            
    t.integer "student_id", null: false                                                                                                                                  
    t.integer "subject_id", null: false                                                                                                                                  
    t.boolean "is_active", default: true, null: false                                                                                                                    
    t.datetime "created_at", null: false                                                                                                                                 
    t.datetime "updated_at", null: false                                                                                                                                 
    t.index ["student_id"], name: "index_student_has_subjects_on_student_id"                                                                                             
    t.index ["subject_id"], name: "index_student_has_subjects_on_subject_id"                                                                                             
    t.index [nil, nil], name: "index_student_has_subjects_on_student_and_subject" #WTF? [nil, nil]                                                                                        
  end

[nil, nil]吓到我了。谁能解释我为什么要代替它?

t.index ["student_id", "subject_id"], name: "index_student_has_subjects_on_student_and_subject"

2 个答案:

答案 0 :(得分:1)

您需要删除此内容...

t.index [:student, :subject]

并添加此...

class CreateStudentHasSubjects < ActiveRecord::Migration[5.2]                                                                                                            
  def change                                                                                                                                                             
    create_table :student_has_subjects do |t|                                                                                                                            
      t.references :student, null: false, foreign_key: true                                                                                                              
      t.references :subject, null: false, foreign_key: true                                                                                                              
      t.boolean :is_active, null: false, default: true                                                                                                                                                                                                                                            

      t.timestamps                                                                                                                                                       
    end  

    add_index :student_has_subjects, [:student, :subject]                                                                                                                                                 
  end                                                                                                                                                                    
end  

答案 1 :(得分:1)

之所以会这样,是因为您使用引用名称而不是列名称。根据{{​​3}},t.index仅支持列名称。

还要注意,如果在student_idsubject_id上添加多列索引,则student_id上的第一个索引可能是多余的。至少PostgreSQL就是这种情况。

相关问题