「Laravelで読書記録システムを作る」に取り組んでいます
(http://learninglaravel.fuwafuwatask.com/migration/)
2018_07_08_070043_create_reading_records_table.php
のマイグレーションをかけたところ、タイトルのようなエラーがでました。
原因はどこにありますでしょうか?
よろしくお願いします
発生している問題・エラーメッセージ
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table reading_records
add constraint reading_records_reader_id_foreign
foreign key (reader_id
) references readers
(id
) on delete cascade)
at /Applications/MAMP/htdocs/board/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e 666| ); 667| } 668|
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
/Applications/MAMP/htdocs/board/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
2 PDOStatement::execute()
/Applications/MAMP/htdocs/board/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
Please use the argument -v to see more details.
該当のソースコード
PHP
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateReadingRecordsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('reading_records', function (Blueprint $table) { 17 $table->engine = 'InnoDB'; 18 $table->increments('id'); 19 $table->integer('book_id')->unsigned(); 20 $table->integer('reader_id')->unsigned(); 21 $table->year('year_read'); 22 $table->tinyInteger('month_read'); 23 $table->string('report'); 24 $table->timestamps(); 25 $table->foreign('book_id') 26 ->references('id') 27 ->on('books') 28 ->onDelete('cascade'); 29 $table->foreign('reader_id') 30 ->references('id') 31 ->on('readers') 32 ->onDelete('cascade'); 33 }); 34 } 35 36 /** 37 * Reverse the migrations. 38 * 39 * @return void 40 */ 41 public function down() 42 { 43 Schema::dropIfExists('reading_records'); 44 } 45} 46
試したこと
$table->engine = 'InnoDB';
を追加で書いてみました
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。