在 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: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"

发生这种情况是因为您使用引用名称而不是列名称。根据源代码,t.index仅支持列名。

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

你需要删除它...

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  

最新更新