質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

Q&A

解決済

2回答

4433閲覧

laravel で seed 時に外部キー制約でエラーになってしまう。

退会済みユーザー

退会済みユーザー

総合スコア0

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

0グッド

0クリップ

投稿2018/12/08 17:50

以下のコードで 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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

データの整合性を保つ外部キーは、
子テーブルを登録する時に親テーブルにキーが存在するかチェックして、子テーブルにテテナシ子状態のデータが登録されるのを排除する機能です。

$table->foreign('trend_id')->references('id')->on('trends');

で、

DB::table('chatrooms')->insert([ 'title' => 'hogehogetitle', 'trend_id' => 0, ]); DB::table('chatrooms')->insert([ 'title' => 'fugafugatitle', 'trend_id' => 1, ]);

だから、

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))

親テーブル trends の id に 0 が登録されていない、って怒られいます。

投稿2018/12/08 23:26

Orlofsky

総合スコア16415

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/12/09 05:21 編集

すみません。incrementが0から始まっているという認識が間違っていました。 解決できました。ありがとうございます。
guest

0

trendsテーブルにはデータ登録されていますでしょうか?

投稿2018/12/08 21:26

karain

総合スコア98

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問