3
1

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 1 year has passed since last update.

【Laravel】PrimaryKeyを外してカラムをnullableにするマイグレーションの書き方

Posted at

概要

PrimaryKeyに指定していたカラムがNULLを許容するためDDLを更新しようとすると以下のエラーが発生したので解決方法をまとめる

 Syntax error or access violation: 1171 All parts of a PRIMARY KEY must be NOT NULL;
ChangeProfileIdToTabletStaffMaps.php
class ChangeProfileIdToTabletStaffMaps extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('tablet_staff_maps', function (Blueprint $table) {
            $table->dropPrimary('tablet_staff_maps_tablet_uuid_staff_id_profile_id_primary');
            $table->dropUnique('tablet_staff_maps_tablet_uuid_staff_id_profile_id_unique');
            $table->primary(['tablet_uuid', 'staff_id']);
            $table->unique(['tablet_uuid', 'staff_id']);
            $table->unsignedBigInteger('profile_id')->comment('プロフィールID')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('tablet_staff_maps', function (Blueprint $table) {
            $table->dropPrimary('tablet_staff_maps_tablet_uuid_staff_id_primary');
            $table->dropUnique('tablet_staff_maps_tablet_uuid_staff_id_unique');
            $table->primary(['tablet_uuid', 'staff_id', 'profile_id']);
            $table->unique(['tablet_uuid', 'staff_id', 'profile_id']);
            $table->unsignedBigInteger('profile_id')->comment('プロフィールID')->change();
        });
    }
}

環境

  • PHP 7.4.25
  • Laravel Framework 6.20.44

解決方法

こちらの記事を参考にしました。
記事の題名通りなのですが処理を分割して記述することが必要になります

ChangeProfileIdToTabletStaffMaps.php
class ChangeProfileIdToTabletStaffMaps extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('tablet_staff_maps', function (Blueprint $table) {
            $table->dropPrimary('tablet_staff_maps_tablet_uuid_staff_id_profile_id_primary');
        });

        Schema::table('tablet_staff_maps', function (Blueprint $table) {
            $table->dropUnique('tablet_staff_maps_tablet_uuid_staff_id_profile_id_unique');
        });

        Schema::table('tablet_staff_maps', function (Blueprint $table) {
            $table->primary(['tablet_uuid', 'staff_id']);
            $table->unique(['tablet_uuid', 'staff_id']);
            $table->unsignedBigInteger('profile_id')->comment('プロフィールID')->nullable()->change();
        });


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('tablet_staff_maps', function (Blueprint $table) {
            $table->dropPrimary('tablet_staff_maps_tablet_uuid_staff_id_primary');
        });

        Schema::table('tablet_staff_maps', function (Blueprint $table) {
            $table->dropUnique('tablet_staff_maps_tablet_uuid_staff_id_unique');
        });

        Schema::table('tablet_staff_maps', function (Blueprint $table) {
            $table->primary(['tablet_uuid', 'staff_id', 'profile_id']);
            $table->unique(['tablet_uuid', 'staff_id', 'profile_id']);
            $table->unsignedBigInteger('profile_id')->comment('プロフィールID')->change();
        });
    }
}
3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?