首页 > 解决方案 > 在 Rails 的多列索引中获取 [nil, nil]

问题描述

我有这个:

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:migrateschema.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"

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

解决方案


你需要删除这个...

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  

推荐阅读