ruby-on-rails - 在 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, :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
推荐阅读
- dji-sdk - 矩阵 600 上的 getWaypointMissionOperator().uploadMission 失败
- c - libmodbus:无法轮询停止位为 2 的 modbus 电能表
- vavr - Has vavr io.vavr.API been removed in 1.0.0-alpha-2?
- reactjs - 为什么我的开关盒只返回一次?
- raspberry-pi - 无法在 Windows IOT Core 中打开或访问 Windows 设备门户
- javascript - 如何在 Firefox 扩展中捕获特定的 xmlhttprequests?
- flutter - Scaffold.of 会找到遥远的小部件 anestors 吗?
- url - 如何使用 TYPO3 9.5 在子页面上发送没有 404 错误的 powermail 表单?
- node.js - 猫鼬是否默认使用有线协议?
- javascript - 更改自动播放策略后如何自动播放音频?