開発環境
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
です。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。