0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelのmigration classでchangeメソッドが使用できない

Last updated at Posted at 2021-03-01

##現象
ある既存のカラムでNULL値を許可orDEFAULT値を設定するようにしたく、
migrationを実行したところ以下のエラーに遭遇。

RuntimeException:Changing columns for table "テーブル名" requires Doctrine DBAL.
Please install the doctrine/dbal package.

##対処法
素直にdoctrine/dbal packageをcomposerでinstallする。

composer require doctrine/dbal
Using version ^3.0 for doctrine/dbal
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Installing composer/package-versions-deprecated (1.11.99.1): Downloading (100%)         
  - Installing doctrine/event-manager (1.1.1): Downloading (100%)         
  - Installing doctrine/cache (1.10.2): Downloading (100%)         
  - Installing doctrine/dbal (3.0.0): Downloading (100%) 
  ...

よし、これでOK。
再度migrationを実行してみます。

Migrating : 2021_03_01_001211_modify_employees_table_20210301

Symfony\Component\Debug\Exception\FatalThrowableError  
: Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found

Driverがないだと。。。
こういう時こそ落ち着いて、エラー文で示してくれているディレクトリの中を覗くと原因が判明する。
vendor/doctrine/dbal/src/Driver/

こちらのパスにPDOMySqlが存在しなかった。

###他にも同様の現象に出くわしている方がいると期待し検索
[laravel] doctrine/dbalを入れたのに「Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found」と言われる

こちらの方が仰られている通り、
ver3.x系(先程installしたversion)とver2.x系とではディレクトリ構成が変わっていました。
気を取り直し、バージョンを指定しinstallし直す。

composer require "doctrine/dbal:2.*"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Downgrading doctrine/dbal (3.0.0 => 2.12.1): Downloading (100%)  
 ...

最後に以下を実行し無事解決に至る。(多謝)

php artisan migrate:refresh --seed
該当のmigrationファイル
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class ModifyEmployeesTable20210301 extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('employees', function (Blueprint $table) {
            $table->string('first_name')->nullable()->change();
            $table->string('last_name')->nullable()->change();
            $table->tinyInteger('email_verified')->default(0)->after('email_verified_at');
            $table->string('email_verify_token')->nullable()->after('email_verified');
            $table->tinyInteger('status')->default(0)->after('email_verify_token');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('employees', function (Blueprint $table) {
            $table->string('first_name')->nullable(false)->change();
            $table->string('last_name')->nullable(false)->change();
            $table->dropColumn('email_verified');
            $table->dropColumn('email_verify_token');
            $table->dropColumn('status');
        });
    }
}

まだまだ未熟者ですが、
経験を蓄積していく為に記事にして残すことを決意した次第です。
今後とも先輩方、宜しくお願い致します。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?