LoginSignup
0
0

More than 3 years have passed since last update.

【laravel+mysql】新規でアプリケーションを立上げ、初めてのphp artisan migrateで詰まった件

Posted at

どんな問題?

初めてPHP言語を使用して、Webアプリケーションを作成するためにlaravelとmysqlで開発環境を整え、新規作成したテーブルをmigrateした際にエラーが発生し、正常にテーブルが作成できない。

エラーの内容

ターミナルで $ php artisan migrateコマンドを入力したところ、以下のようなエラーが発生しました。
6行目の通り、1050 Table 'users' already exists...とあるように「既にuserテーブルは存在あるよ」ということです。

ターミナル
$ php artisan migrate
Migrating: 2014_10_12_000000_create_users_table

   Illuminate\Database\QueryException 

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table `users` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) not null, `email_verified_at` timestamp null, `password` varchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675| 

      +9 vendor frames 
  10  database/migrations/2014_10_12_000000_create_users_table.php:24
      Illuminate\Support\Facades\Facade::__callStatic("create")

      +22 vendor frames 
  33  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

前提

macOS ver10.15.4
laravel ver4.0.0導入済み
mysql ver5.6.47導入済み
sequelproに接続済み

エラー発生までの流れ

ターミナル上で

ターミナル
$ laravel new [プロジェクト名]

コマンドで新規プロジェクト作成した際、userテーブルも自動生成されます。(password_resetsテーブルも自動生成されます。)

今回、追加でitemテーブルを作ろうと思い、

ターミナル
$ php artisan make:migration create_items_table

でitemテーブルのマイグレーションファイルを作成し、

ターミナル
$ php artisan migrate

を実行したところ、上記の通り1050 Table 'users' already exists...というエラーが出て、itemテーブルが作成できませんでした。

対処方法

userマイグレーションファイルを以下のように変更しました。

変更前

userマイグレーションファイル
上段省略
class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
以下省略

変更後

userマイグレーションファイル
上段省略
class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('users')) {
            // usersテーブルが存在していればリターンする
            return;
        }
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
以下省略

userのマイグレーションファイルのpublic function up() の中身に

userマイグレーションファイル
if (Schema::hasTable('users')) {
            // usersテーブルが存在していればリターンする
            return;

の記述を入れて存在しているテーブルをリターンさせたところ、migrateが通りました。

なお、userを解決した後、password_resetsテーブルについても同様の現象が発生したため、同じ要領で対応しました。

最後に

phpに関しては初学者のため、まだまだ解っていないことが多いので、他に最善な解決方法等がありましたらご指摘いただけますと幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0