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

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

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

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

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

MAMP

Mac 上で WordPress などの動的ページのサイトが作れるように環境を構築するフリーソフト

Q&A

解決済

1回答

1542閲覧

laravelシーディングの際にエラーが表示される

ishidakouiki

総合スコア73

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

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

MAMP

Mac 上で WordPress などの動的ページのサイトが作れるように環境を構築するフリーソフト

0グッド

0クリップ

投稿2022/02/04 07:59

開発環境
php バージョン=PHP 7.3.29
laravelバージョン=laravel5.8
MAMP
vscode

質問掲示板サイトのデータベースのシーディングを実行したら、

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'aaaa@aaaa.com' for key 'users_email_unique' (SQL: insert into `users` (`name`, `email`, `password`) values (aaaa, aaaa@aaaa.com, $2y$10$FOXpx9vCw.4KI1P0kAdda.siH3WwwxXFiUXdY6UzDGtQA8f3YqZ5K))

とエラーが表示されます。
テーブルは、
uses、questions、comments、othersの4つで、
先に、usesz→questions→commentsの順にマイグレーション、シーディングを実行して、最後に、othersテーブルのマイグレーションを実行。シーディングを実行したらエラーが表示されます。

【テーブルファイル】

users_table

1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateUsersTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('users', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->string('name'); 19 $table->string('email')->unique(); 20 $table->timestamp('email_verified_at')->nullable(); 21 $table->string('password'); 22 $table->rememberToken(); 23 $table->timestamps(); 24 }); 25 } 26 27 /** 28 * Reverse the migrations. 29 * 30 * @return void 31 */ 32 public function down() 33 { 34 Schema::dropIfExists('users'); 35 } 36} 37

questions_table

1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateQuestionsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('questions', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->unsignedBigInteger('user_id'); 19 $table->string('title', 20); 20 $table->string('problem',1000); 21 $table->string('problemsolving',1000); 22 $table->string('execution',1000); 23 $table->timestamps(); 24 25 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 26 }); 27 } 28 29 /** 30 * Reverse the migrations. 31 * 32 * @return void 33 */ 34 public function down() 35 { 36 Schema::dropIfExists('questions'); 37 } 38} 39

comments_table

1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateCommentsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('comments', function (Blueprint $table) { 17 $table->Increments('id'); 18 $table->unsignedBigInteger('user_id'); 19 $table->unsignedBigInteger('question_id'); 20 $table->string('comment', 500); 21 $table->timestamps(); 22 23 $table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade'); 24 }); 25 } 26 27 /** 28 * Reverse the migrations. 29 * 30 * @return void 31 */ 32 public function down() 33 { 34 Schema::dropIfExists('comments'); 35 } 36} 37

others_table

1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateOthersTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('others', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->unsignedBigInteger('user_id'); 19 $table->string('title', 20); 20 $table->string('text',1000); 21 $table->timestamps(); 22 23 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 24 }); 25 } 26 27 /** 28 * Reverse the migrations. 29 * 30 * @return void 31 */ 32 public function down() 33 { 34 Schema::dropIfExists('others'); 35 } 36} 37

password_table

1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreatePasswordResetsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('password_resets', function (Blueprint $table) { 17 $table->string('email')->index(); 18 $table->string('token'); 19 $table->timestamp('created_at')->nullable(); 20 }); 21 } 22 23 /** 24 * Reverse the migrations. 25 * 26 * @return void 27 */ 28 public function down() 29 { 30 Schema::dropIfExists('password_resets'); 31 } 32} 33

【シーダーファイル】

Users.seeder

1<?php 2 3use Illuminate\Database\Seeder; 4 5class UsersTableSeeder extends Seeder 6{ 7 /** 8 * Run the database seeds. 9 * 10 * @return void 11 */ 12 public function run() 13 { 14 DB::table('users')->insert([ 15 'name' => 'aaaa', 16 'email' => 'aaaa@aaaa.com', 17 'password' => bcrypt('aaaa') 18 ]); 19 20 DB::table('users')->insert([ 21 'name' => 'bbbb', 22 'email' => 'bbbb@bbbb.com', 23 'password' => bcrypt('bbbb') 24 ]); 25 26 DB::table('users')->insert([ 27 'name' => 'cccc', 28 'email' => 'cccc@cccc.com', 29 'password' => bcrypt('cccc') 30 ]); 31 } 32}

questions.seeder

1<?php 2 3use Illuminate\Database\Seeder; 4 5class QuestionsTableSeeder extends Seeder 6{ 7 /** 8 * Run the database seeds. 9 * 10 * @return void 11 */ 12 public function run() 13 { 14 for ($i = 1; $i <= 100; $i++ ){ 15 16 $title = str_random(20); 17 $problem = str_random(1000); 18 $problemsolving = str_random(1000); 19 $execution = str_random(1000); 20 $data = 21 [ 22 'user_id' =>rand(1,2), 23 'title' => $title, 24 'problem' => $problem, 25 'problemsolving' => $problemsolving, 26 'execution' => $execution, 27 'created_at' => now(), 28 'updated_at' => now(), 29 ]; 30 31 DB::table('questions')->insert($data); 32 } 33 } 34} 35

comments.seeder

1<?php 2 3use Illuminate\Database\Seeder; 4 5class CommentsTableSeeder extends Seeder 6{ 7 /** 8 * Run the database seeds. 9 * 10 * @return void 11 */ 12 public function run() 13 { 14 for ($i = 1; $i <= 100; $i++) { 15 16 $rand500 = str_random(500); 17 18 $params = [ 19 'comment' => $rand500, 20 'question_id' => $i, 21 'user_id' => rand(1,3), 22 'created_at' => now(), 23 'updated_at' => now(), 24 ]; 25 26 DB::table('comments')->insert($params); 27 } 28 } 29} 30

others.seeder

1<?php 2 3use Illuminate\Database\Seeder; 4 5class OthersTableSeeder extends Seeder 6{ 7 /** 8 * Run the database seeds. 9 * 10 * @return void 11 */ 12 public function run() 13 { 14 for ($i = 1; $i <= 100; $i++ ){ 15 16 $title = str_random(20); 17 $text = str_random(1000); 18 $data = 19 [ 20 'user_id' => rand(a,b), 21 'title' => $title, 22 'text' => $text, 23 'created_at' => now(), 24 'updated_at' => now(), 25 ]; 26 27 DB::table('others')->insert($date); 28 } 29 } 30} 31

Database.seeder

1<?php 2 3use Illuminate\Database\Seeder; 4 5class DatabaseSeeder extends Seeder 6{ 7 /** 8 * Seed the application's database. 9 * 10 * @return void 11 */ 12 public function run() 13 { 14 $this->call(UsersTableSeeder::class); 15 $this->call(QuestionsTableSeeder::class); 16 $this->call(CommentsTableSeeder::class); 17 $this->call(OthersTableSeeder::class); 18 } 19} 20

です。

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

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

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

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

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

guest

回答1

0

ベストアンサー

Duplicate entry 'aaaa@aaaa.com' for key 'users_email_unique'

がすべてだと思います。このワードで検索とかされたでしょうか? 重複したデータを入れられないというデータベースの制限に引っかかっているのだと思います。シーディングということなので、すでに入ってるデータベースのデータを一度削除してからやる、もしくはシーディングデータの重複を取り除くのいずれかになると思います。

投稿2022/02/04 08:13

AbeTakashi

総合スコア4537

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問