Я работаю над проектом 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
Возможно, есть два разных подхода, которые вы должны попробовать:
Во-первых, попробуйте применить значение integer перед назначением:
information.program_id = information.system_plan.to_i
Во-вторых, попробуйте назначить объект
information.program = Program.find(information.system_plan.to_i)
Помимо этих 2, я не мог видеть другие неправильные действия в вашем коде.
id=2879
в вашей базе данных?