Error renaming existing column in Laravel migration

I added a new migration to rename the old column. Everything seems to be correct in this code, for me:

public function up()
{
   Schema::table('reports', function (Blueprint $table) {
        $table->renameColumn('reporter_id', 'created_by');
    });
}

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

But then I ran into an error:

In Connection.php line 664: SQLSTATE[0A000]: Feature not supported: 1846 ALGORITHM=COPY is not supported. Reason: Columns participating in a foreign key are renamed. Try ALGORITHM=INPLACE. (SQL: ALTER TABLE reports CHANGE reporter_id created_b    y INT NOT NULL)                                                                                                                                                                                                          
In PDOStatement.php line 140: SQLSTATE[0A000]: Feature not supported: 1846 ALGORITHM=COPY is not supported. Reason: Columns participating in a foreign key are renamed. Try ALGORITHM=INPLACE.
In PDOStatement.php line 138: SQLSTATE[0A000]: Feature not supported: 1846 ALGORITHM=COPY is not supported. Reason: Columns participating in a foreign key are renamed. Try ALGORITHM=INPLACE. `

Could you help me fix this?

+4
source share
2 answers

The first drop koreign keyon up.

public function up()
{
  Schema::table('reports', function (Blueprint $table) {
     $table->dropForeign('reports_reporter_id_foreign');
     $table->renameColumn('reporter_id', 'created_by');
  });
}

Then add foreign keyagain to the method down.

public function down()
{
    Schema::table('reports', function (Blueprint $table) {
        $table->renameColumn('created_by', 'reporter_id');
        $table->foreign('reporter_id')->references('id')->on('your_related_table')->onDelete('cascade');
    });
}
+1
source

I also came across this - this is pointless because when I use my standard SQL client to rename the same field ... it works. But that just doesn't work as a migration script. So I finished the work RENAMEinternally DB::statementand worked for me:

     /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        DB::statement("ALTER TABLE `mydb`.`mytable`   
          CHANGE `currentfieldname` `newfieldname` 
          INT(10) UNSIGNED NOT NULL;");

    }
0

Source: https://habr.com/ru/post/1695799/