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

问题描述 投票:1回答:2

我有这个:

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"
ruby-on-rails ruby activerecord ruby-on-rails-5
2个回答
1
投票

发生这种情况是因为您使用引用名称而不是列名称。根据source codet.index只支持列名。

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


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  
© www.soinside.com 2019 - 2024. All rights reserved.