2
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で文字列型カラムから数値型への変更でハマった解決策

Last updated at Posted at 2020-02-17

マイグレーションで数値型→文字列型へのカラム変更が必要になり、変更時は問題なかったものの、rollback時に以下のエラーが発生。

 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064
 You have an error in your SQL syntax; 
check the manual that corresponds to your MariaDB server version for the right syntax to use near 
'CHARACTER SET utf8mb4 NOT NULL COLLATE `utf8mb4_unicode_ci`, CHANGE fax fax BIGI' at line 1")

原因がわからず困っていたが、よく読むとcharsetが云々書いてあったので、↓で文字列型の時に残っているcahrsetをnullにして解決!


class AddStelToUsersTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('s_tel')->nullable()->after('tel');
            $table->string('tel')->change();
            $table->string('fax')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('s_tel');
            $table->bigInteger('tel')->charset(null)->change();
            $table->bigInteger('fax')->charset(null)->nullable()->change();
        });
    }
}

2
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
2
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?