Getting [nil, nil] in multicolumn index in Rails

I have this:

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        

and when I execute $ rails db:migrate I get, in the schema.rb file:

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

That [nil, nil] kind of scares me. Can anyone explain me why do I get that instead of:

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

Upvotes: 1

Views: 111

Answers (2)

Matthieu Libeer
Matthieu Libeer

Reputation: 2365

This happens because you use the reference names instead of the column names. According to the source code, t.index only supports columns names.

Also note that if you add a multi-column index on student_id and subject_id, the first index on student_id is probably redundant. That's the case for PostgreSQL at least.

Upvotes: 1

Verty00
Verty00

Reputation: 743

you need to remove this...

t.index [:student, :subject]

and add this...

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  

Upvotes: 1

Related Questions