【Laravelマイグレーション】DBのテーブル名変更・カラム名変更・型変更

※本サイトはアフィリエイトリンクを掲載しています。
Laravel

DB(データベース)のテーブル名を変更する

今回はgoodsテーブル(商品テーブル)からproductsテーブル(商品テーブル)にリネームします。
 

実現方法手順①:変更用のマイグレーションファイルを作成する

php artisan make:migration:rename_goods_to_products_table --table=goods

‐‐table=テーブル名で、どのテーブルに対するマイグレーションファイルなのかを指定できる。

 

実現方法手順②:マイグレーションファイルを修正する

修正後

<?php

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

class RenameGoodsToProductsTable extends Migration
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    { 
        Schema::rename('goods','products');
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        Schema::rename('products','goods');
    }
}

 

テーブル名のリネームコード

Schema::rename('変更前テーブル名','変更後テーブル名');

 

downメソッドの場合はリネームを元に戻すため、下記になります。

Schema::rename('変更後テーブル名', '変更前テーブル名');

 

DB(データベース)のカラム名を変更したい

今回はcostカラム(値段)をpriceカラム(値段)にリネームします。
 

実現方法手順①:変更用のマイグレーションファイルを作成する

php artisan make:migration:rename_column_cost_to_price_on_products_table --table=products

‐‐table=テーブル名で、どのテーブルに対するマイグレーションファイルなのかを指定できる。

 

実現方法手順②:マイグレーションファイルを修正する

修正後

<?php

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

class RenameColumnCostToPriceTable extends Migration
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {
        Schema::table('prodcts', function (Blueprint $table) {
            $table->renameColumn('cost', 'price');
        });
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        Schema::table('prodcts', function (Blueprint $table) {
            $table->renameColumn('price', 'cost');
        });
    }
}

 

カラム名変更の時に、エラーになるケースがあります。

カラム名変更用のマイグレーションファイルを実行する時に、下記エラーになる場合があります。

 

エラー内容

① Class ‘Doctrine\DBAL\Driver\AbstractMySQLDriver’ not found

Doctrine DBAL Driver PDOMySql Driver not found

○○requires Doctrine DBAL.

 

解決策

composer require doctrine/dbal

上記コマンドを実行後に、もう一度マイグレーション実行してみましょう。

 

なぜエラーになるのか

エラー内容は「必要なドライバーがないので処理ができない」という趣旨のエラーです。

そのため、コンポーザーに必要なものをインストールし直すコマンドを実行する必要があります。

 

DB(データベース)のカラムの型を変更する

今回はpriceカラムの型をstringからintegerに変更します。
 

実現方法手順①:変更用のマイグレーションファイルを作成する

php artisan make:migration:change_price_column_string_to_integer_table --table=products

‐‐table=テーブル名で、どのテーブルに対するマイグレーションファイルなのかを指定できる。

 

実現方法手順②:マイグレーションファイルを修正する

<?php

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

class PriceColumnStringToIntegerTable extends Migration
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {
        Schema::table('prodcts', function (Blueprint $table) {
            $table->integer('price')->change();
        });
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        Schema::table('prodcts', function (Blueprint $table) {
            $table->string('price')->change();
        });
    }
}

コメント