我正在开发Ruby on Rails项目,在为外键分配值时出现以下错误:
ActiveRecord::InvalidForeignKey in InformationController#import
Mysql2::Error: Cannot add or update a child row: a foreign key
constraint fails (`sobrecupos_development`.`information`, CONSTRAINT
`fk_rails_21c7b6e26c` FOREIGN KEY (`program_id`) REFERENCES `programs`
(`id`)): UPDATE `information` SET `system_plan` = '2879.0',
`program_id` = 2879, `updated_at` = '2018-04-03 15:32:07' WHERE
`information`.`id` = 11
这个密钥应该从外部文件中分配,但是当我尝试这样做时,我得到了上面提到的错误。我试图明确地分配一个键来检查它是否与可能的非驱动值有关,但我仍然得到这个错误。
这是我的schema.rb文件
ActiveRecord::Schema.define(version: 20180402215605) do
create_table "coordinators", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.string "lastname"
t.integer "department_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "username"
t.index ["department_id"], name: "index_coordinators_on_department_id", using: :btree
t.index ["email"], name: "index_coordinators_on_email", unique: true, using: :btree
t.index ["username"], name: "index_coordinators_on_username", unique: true, using: :btree
end
create_table "departments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "code"
t.string "abbreviation"
t.index ["code"], name: "index_departments_on_code", unique: true, using: :btree
end
create_table "groups", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "subject_id"
t.integer "professor_id"
t.integer "available_spots"
t.integer "total_spots"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["professor_id"], name: "index_groups_on_professor_id", using: :btree
t.index ["subject_id"], name: "index_groups_on_subject_id", using: :btree
end
create_table "information", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "level"
t.integer "program_id"
t.string "request_type"
t.string "act_resolution"
t.date "resolution_date"
t.string "dependency"
t.decimal "PAPA", precision: 10, scale: 1
t.integer "registered_credits"
t.integer "outstanding_credits"
t.decimal "completion_percentage", precision: 10
t.integer "tuition_amount"
t.string "academic_history_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "student_id"
t.boolean "was_saved"
t.string "system_plan"
t.string "academic_period"
t.string "document"
t.index ["program_id"], name: "index_information_on_program_id", using: :btree
t.index ["student_id"], name: "index_information_on_student_id", using: :btree
end
create_table "professors", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "programs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.integer "department_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "code"
t.string "program_type"
t.index ["code"], name: "index_programs_on_code", unique: true, using: :btree
t.index ["department_id"], name: "index_programs_on_department_id", using: :btree
end
create_table "students", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.string "lastname"
t.string "username"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "document"
t.index ["email"], name: "index_students_on_email", unique: true, using: :btree
t.index ["username"], name: "index_students_on_username", unique: true, using: :btree
end
create_table "subject_requests", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "group_id"
t.integer "information_id"
t.boolean "was_canceled"
t.string "approbation_state"
t.string "inscription_state"
t.string "replace_to"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "accept_other_group"
t.string "subject_code"
t.string "group_code"
t.string "typology"
t.string "subject_plan"
t.index ["group_id"], name: "index_subject_requests_on_group_id", using: :btree
t.index ["information_id"], name: "index_subject_requests_on_information_id", using: :btree
end
create_table "subjects", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.integer "department_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "code"
t.integer "credits"
t.index ["code"], name: "index_subjects_on_code", unique: true, using: :btree
t.index ["department_id"], name: "index_subjects_on_department_id", using: :btree
end
add_foreign_key "coordinators", "departments"
add_foreign_key "groups", "professors"
add_foreign_key "groups", "subjects"
add_foreign_key "information", "programs"
add_foreign_key "information", "students"
add_foreign_key "programs", "departments"
add_foreign_key "subject_requests", "groups"
add_foreign_key "subject_requests", "information", on_delete: :cascade
add_foreign_key "subjects", "departments"
end
这里是我尝试为外键分配值的代码行
information.program_id = information.system_plan
答案 0 :(得分:0)
也许你应该尝试两种不同的方法:
首先,在分配之前尝试强制转换为整数:
information.program_id = information.system_plan.to_i
其次,尝试分配对象
information.program = Program.find(information.system_plan.to_i)
除了这两个,我在你的代码中看不到其他错误的行为。