以下のコードで laravel で seed がしたいのですがうまく行きませんわかる方、ご教示いただけませんか?
migration
1 Schema::create('chatrooms', function (Blueprint $table) { 2 $table->increments('id'); 3 $table->string('title'); 4 $table->integer('trend_id')->unsigned(); 5 $table->foreign('trend_id')->references('id')->on('trends'); 6 $table->timestamps(); 7 });
seeder
1public function run() 2 { 3 DB::table('chatrooms')->insert([ 4 'title' => 'hogehogetitle', 5 'trend_id' => 0, 6 ]); 7 DB::table('chatrooms')->insert([ 8 'title' => 'fugafugatitle', 9 'trend_id' => 1, 10 ]); 11 }
以下、エラーコードになります。
error
1 Illuminate\Database\QueryException : SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`default`.`chatrooms`, CONSTRAINT `chatrooms_trend_id_foreign` FOREIGN KEY (`trend_id`) REFERENCES `trends` (`id`)) (SQL: insert into `chatrooms` (`title`, `trend_id`) values (hogehogetitle, 0)) 2 3 at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664 4 660| // If an exception occurs when attempting to run a query, we'll format the error 5 661| // message to include the bindings with SQL, which will make this exception a 6 662| // lot more helpful to the developer instead of just the database's errors. 7 663| catch (Exception $e) { 8 > 664| throw new QueryException( 9 665| $query, $this->prepareBindings($bindings), $e 10 666| ); 11 667| } 12 668| 13 14 Exception trace: 15 16 1 PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`default`.`chatrooms`, CONSTRAINT `chatrooms_trend_id_foreign` FOREIGN KEY (`trend_id`) REFERENCES `trends` (`id`))") 17 /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458 18 19 2 PDOStatement::execute() 20 /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458 21 22 Please use the argument -v to see more details. 23
回答2件
あなたの回答
tips
プレビュー