Home>

I'm in trouble because I can't execute "drioForeign" to remove foreign key constraints in laravel.

・ Status
The situation is exactly the same as the following qiita article.
Set onDelete/onUpdate of foreign key constraints after migration with Laravel + logical delete I was addicted

In other words, because I set a foreign key constraint without the onDelete option, I want to remove the foreign key constraint and set it again.
The official reference also states that it can be canceled by putting "'table name_column name_foreign'" in dropForeign.

Your laravelver5.8 reference

It was

, but when I actually tried to create a migration file and run it, an error occurred and I could not run it.
Error screen ↓
] (6b9721a1218fcf9f4c3e78bcee81a14f.png)

The table was actually show indexed and compared with the description in the migration file.
Column details ↓

Code in migration file ↓

<? php
use Illuminate \ Support \ Facades \ Schema;
use Illuminate \ Database \ Schema \ Blueprint;
use Illuminate \ Database \ Migrations \ Migration;
class ChangeDrillsDropForeignCategoryId extends Migration
{
    / **
     * Run the migrations.
     *
     * @return void
     * /
    public function up ()
    {
        Schema :: table ('drills', function (Blueprint $table) {
            $table->dropForeign ('drills_cateory_id_foreign');
        });
    }
    / **
     * Reverse the migrations.
     *
     * @return void
     * /
    public function down ()
    {
        Schema :: table ('drills', function (Blueprint $table) {
            //
        });
    }
}


・ Other points to remember
Although it is the categories table that is the external constraint destination of category_id of drills,
When the external constraint was initially set, it was named "caterorys".
Later renamed "categories" due to the SQL issued by laravel.

・ What I did
I thought that it should be changed from "categories" (current table name) to "categorys" (table name at the time of external constraint setting) in order to release the external constraint, so I tried dropForeign after renaming the name.

can't be executed with an error.

I'm sorry for the long sentence, but if you give me some advice, I'll be saved!

  • Answer # 1

    Self-solved.

    At the time of the image shown in the details of the column, it seems that the foreign key constraint was released.
    When using dropForeign method, it seems that the key name remains in key_name even if the constraint is removed.

    Check the details of the table in SHOW CREATE TABLES,
    We were able to confirm exactly the external reference whether the table and the column to be referred to were specified in the "CONSTRAINT" column.

Trends