Dropping column with foreign key Laravel error: General error: 1025 Error on rename

前端 未结 5 588
北海茫月
北海茫月 2020-12-13 05:39

I\'ve created a table using migration like this:

public function up()
{
    Schema::create(\'despatch_discrepancies\',  function($table) {
        $table->         


        
5条回答
  •  余生分开走
    2020-12-13 05:50

    I had multiple foreign keys in my table and then I had to remove foreign key constraints one by one by passing column name as index of the array in down method:

    public function up()
    {
        Schema::table('offices', function (Blueprint $table) {
            $table->unsignedInteger('country_id')->nullable();
            $table->foreign('country_id')
                ->references('id')
                ->on('countries')
                ->onDelete('cascade');
    
            $table->unsignedInteger('stateprovince_id')->nullable();
            $table->foreign('stateprovince_id')
                ->references('id')
                ->on('stateprovince')
                ->onDelete('cascade');
            $table->unsignedInteger('city_id')->nullable();
            $table->foreign('city_id')
                ->references('id')
                ->on('cities')
                ->onDelete('cascade');
        });
    }
    
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('offices', function (Blueprint $table) {
            $table->dropForeign(['country_id']);
            $table->dropForeign(['stateprovince_id']);
            $table->dropForeign(['city_id']);
            $table->dropColumn(['country_id','stateprovince_id','city_id']);
        });
    } 
    

    Using below statement does not work

    $table->dropForeign(['country_id','stateprovince_id','city_id']); 
    

    Because dropForeign does not consider them seperate columns that we want to remove. So we have to drop them one by one.

提交回复
热议问题