我是 laravel 和一般編程的新手,在我的 laravel 專案中遷移時遇到問題。我正在嘗試為“問題”和“問題類別”表之間的一對多關系創建一個常見問題資料庫。我似乎無法在沒有錯誤的情況下遷移它。這是下面的錯誤,
SQLSTATE[HY000]: General error: 1822 Failed to add the foreign key constraint. Missing index for constraint 'questions_question_category_id_foreign' in the referenced table 'question_category' (SQL: alter table `questions` add constraint `questions_question_category_id_foreign` foreign key (`question_category_id`) references `question_category` (`id`) on delete cascade)
如果有人可以提供幫助,那將有很大幫助。謝謝你。
question_category 表/遷移
Schema::create('question_category', function (Blueprint $table) {
$table->integer('id');
$table->string('question_category_title');
$table->mediumText('description')->nullable();
$table->timestamps();
});
問題表/遷移
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->string('question');
$table->mediumText('answer')->nullable();
$table->timestamps();
$table->integer('question_category_id');
$table->foreign('question_category_id')
->references('id')
->on('question_category')
->onDelete('cascade');
});
uj5u.com熱心網友回復:
更改問題表中外鍵欄位的資料型別
$table->unsignedBigInteger('question_category_id');
更改問題類別表中的主鍵資料型別
$table->bigIncrements('id');
uj5u.com熱心網友回復:
用問題表/遷移替換它
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->string('question');
$table->mediumText('answer')->nullable();
$table->timestamps();
$table->integer('question_category_id')->unsigned();
$table->foreign('question_category_id')
->references('id')
->on('question_category')
->onDelete('cascade');
});
現在將兩個表都轉儲為question_categoryandquestions或使用php artisan migrate:fresh(這將重新遷移您的表)并檢查它。謝謝你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/407377.html
標籤:
