前提
Laravelの練習のために基本的な機能を持つデータ入力システムを作成しています。
「php artisan migrate」によってmigrateを行いましたが、エラーメッセージが出てしまうため、解決したいです。
実現したいこと
マイグレーションファイルの生成を行いたいです。
発生している問題・エラーメッセージ
※コマンドプロンプトで「php artisan migrate」を行った時のエラー内容です。
cmd
1 INFO Running migrations. 2 2022_11_07_030830_create_people .......................................................................... 64ms FAIL 3 Illuminate\Database\QueryException 4 SQLSTATE[HY000]: General error: 1 table "people" already exists (SQL: create table "people" ("id" integer not null primary key autoincrement, "id" integer not null primary key autoincrement, "name" varchar not null, "mail" varchar not null, "memo" varchar not null, "age" integer not null, "created_at" datetime, "updated_at" datetime)) 5 at C:\Users\murai\Documents\Laravelapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:760 6 756▕ // If an exception occurs when attempting to run a query, we'll format the error 7 757▕ // message to include the bindings with SQL, which will make this exception a 8 758▕ // lot more helpful to the developer instead of just the database's errors. 9 759▕ catch (Exception $e) { 10 ➜ 760▕ throw new QueryException( 11 761▕ $query, $this->prepareBindings($bindings), $e 12 762▕ ); 13 763▕ } 14 764▕ } 15 1 C:\Users\murai\Documents\Laravelapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:539 16 PDOException::("SQLSTATE[HY000]: General error: 1 table "people" already exists") 17 2 C:\Users\murai\Documents\Laravelapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:539 18 PDO::prepare("create table "people" ("id" integer not null primary key autoincrement, "id" integer not null primary key autoincrement, "name" varchar not null, "mail" varchar not null, "memo" varchar not null, "age" integer not null, "created_at" datetime, "updated_at" datetime)")
関連ファイルのソースコード
C:\Users\murai\Documents\Laravelapp\database\migrations
2022_11_07_030830_create_people.php
1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7return new class extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('people', function (Blueprint $table) { 17 $table->id(); 18 19 $table->increments('id'); 20 $table->string('name'); 21 $table->string('mail'); 22 $table->string('memo'); 23 $table->integer('age'); 24 25 $table->timestamps(); 26 }); 27 } 28 29 /** 30 * Reverse the migrations. 31 * 32 * @return void 33 */ 34 public function down() 35 { 36 Schema::dropIfExists('people'); 37 } 38};
補足情報(FW/ツールのバージョンなど)
Laravel Framework 9.38.0
PDOException::("SQLSTATE[HY000]: General error: 1 table "people" already exists")
に原因がまるっとそのまま書いてありますが・・・
エラーメッセージは読んでませんか?
それともこのエラーの内容の意味は分かっている上で、その対応方法が分からないということでしょうか?
コメントありがとうございます。
「Peopleテーブルはすでに存在する」という部分がエラー原因であると認識しています。
そのうえで対処が分からない状況です。(エラーの把握も確かな自信はありません)
エラー文を検索などしてみたのですが、現状打破できず苦慮しています。
※書き忘れていましたが、私の環境はWindows11となります。
作りたいテーブルがすでにあるならマイグレーションする必要ないのでは。
どういう結果(今回は生成内容?)を期待しているのでしょうか。
コメントありがとうございます。
マイグレーションを試すために、databaseフォルダ内のdatabase.sqliteファイルを一旦ほかの場所へ移動してから、そこへ空のデータベースファイルを新たに作成し(その際、touchコマンドが使えない為、代わりにtype nulコマンドを使用)、migrateをためした結果このエラーになった流れです。
今は参考にしている書籍を読み進めながらなぞっており、以下の結果を得ることができず試行錯誤しています。
・期待している結果は以下のマイグレーションが実行されることです。
```cmd
Migration table created susessfully
Migrating: xxxx_create_users_table
Migrated: xxxx_create_users_table
Migrating: xxxx_create_password_reset_table
Migrated: xxxx_create_password_reset_table
Migrating: xxxx_create_people_table
Migrated: xxxx_create_people_table
```
エラー内容を認識しつつ対応方法が分からない件や環境情報(SQLiteやWindowsを利用してる)はかなり大事な情報なので質問文に追記した方がいいと思います。あとタイトルも「マイグレーションファイルの生成」よりは「マイグレーションの実行」ですよね。ちょっと紛らわしい感じを受けました。
自分はLaravelでSQLite使ったことないのでその状況はよく分かりませんが、単純にConfigで設定しているdatabase.sqliteとは別のファイルを移動してるのでは?とか、あとはConfigがしっかり反映されてない可能性があるので php artisan config:cache してみるとかくらいしか思いつきません。
質問は編集できますので、本文に追記を。