前提・実現したいこと
Laravelで仮想店舗のデータベースを作ろうと試みているのですが、外部キー制約で違反が出てしまい解決方法がわからずはまってしまっています。Booksテーブルが親、Authorsテーブルが子テーブルにしているつもりです。
BooksテーブルとAuthorsテーブルを連携させて、IDが一致するようにしたいです。初心者のため、何が原因なのかわかりません。どうかご教授お願いいたします。
発生している問題・エラーメッセージ
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint falis ('test'. 'authors', CONSTRAINT 'authors_著者_id_foreign' FOREIGN KEY ('著者ID') REFERENCES 'books' ('書籍ID')) (SQL: insert into 'authors' ('著者名') values (尾田栄一郎))
実行コマンドはこちらです。
PHP
1php artisan db:seed --class=AuthorsTableSeeder
###結果
前述のエラーが発生し、BooksテーブルにもAuthorsテーブルにも値が何も入っておらず更新されていません。
該当のソースコード
関連するファイルを一つずつ記載いたします。
1.Eloquentモデルクラス
・Books.php
・Authors.php
2.マイグレーションファイル
・Booksテーブルのマイグレーションファイル
・Authorsテーブルのマイグレーションファイル
3.シーディングのためのクラス
・AuthorsTableSeeder.php
1.Eloquentモデルクラス
・Books.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Books extends Model 8{ 9 10 public function authors() 11 { 12 return $this->hasMany(Authors::class); 13 } 14}
・Authors.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Authors extends Model 8{ 9 public function books() 10 { 11 return $this->belongsTo(Books::class); 12 } 13} 14
2.マイグレーションファイル
・Bookテーブルのマイグレーションファイル
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateBooksTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('books', function (Blueprint $table) { 17 $table->increments('書籍ID'); 18 $table->string('書籍名'); 19 $table->integer('著者ID'); 20 $table->string('著者名'); 21 $table->integer('出版社ID'); 22 $table->string('出版社名'); 23 $table->integer('ジャンルID'); 24 $table->string('ジャンル名'); 25 $table->integer('価格'); 26 $table->dateTime('発行年月'); 27 $table->integer('ISBNコード'); 28 $table->timestamps(); 29 }); 30 } 31 32 /** 33 * Reverse the migrations. 34 * 35 * @return void 36 */ 37 public function down() 38 { 39 Schema::dropIfExists('books'); 40 } 41} 42
・Authorsテーブルのマイグレーションファイル
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateAuthorsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('authors', function (Blueprint $table) { 17 $table->increments('著者ID'); 18 $table->string('著者名'); 19 $table->timestamps(); 20 21 $table->foreign('著者ID') 22 ->references('書籍ID') 23 ->on('books') 24 ->onDelete('restrict'); 25 }); 26 } 27 28 /** 29 * Reverse the migrations. 30 * 31 * @return void 32 */ 33 public function down() 34 { 35 Schema::dropIfExists('authors'); 36 } 37} 38
3.シーディングのためのクラス
・AuthorsTableSeeder.php
php
1<?php 2 3use Illuminate\Database\Seeder; 4 5class AuthorsTableSeeder extends Seeder 6{ 7 /** 8 * Run the database seeds. 9 * 10 * @return void 11 */ 12 public function run() 13 { 14 $param = [ 15 '著者名' => '尾田栄一郎', 16 ]; 17 DB::table('authors')->insert($param); 18 19 $param = [ 20 '著者名' => '村上春樹', 21 ]; 22 DB::table('authors')->insert($param); 23 } 24} 25
試したこと
以前に似たような質問をしている方がいらっしゃったのでその解決方法を試してみました。
解決方法は、Booksモデルクラスにpublic $incrementing = false;
を追加してみましたが何も変わりませんでした。
##※追記
php
1$param = [ 2 '著者ID' => '1', ←を足しました。 3 '著者名' => '尾田栄一郎', 4]; 5DB::table('authors')->insert($param);
実行コマンドは通常通り
php artisan db:seed
です。
補足情報(FW/ツールのバージョンなど)
・Laravel5.5
・PHP
・MariaDB
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/25 04:52
2018/01/25 06:33
2018/01/25 13:50
2018/01/26 00:30
2018/01/26 03:48
2018/01/26 04:58
2018/01/26 06:33