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

前端 未结 5 543
北海茫月
北海茫月 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.

    0 讨论(0)
  • 2020-12-13 05:58

    Something that occurred to me was that I didn't know where to put the Schema::table block.

    Later I discovered that the key is on the SQL error:

    [Illuminate\Database\QueryException]
    SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists `lu_benefits_categories`)
    

    So the Schema::table block needs to go in the down() function of the lu_benefits_categories migration and before the Schema::dropIfExists line:

    public function down()
    {
        Schema::table('table', function (Blueprint $table) {
            $table->dropForeign('table_category_id_foreign');
            $table->dropColumn('category_id');
        });
        Schema::dropIfExists('lu_benefits_categories');
    }
    

    After that, the php artisan migrate:refresh or php artisan migrate:reset will do the trick.

    0 讨论(0)
  • 2020-12-13 06:03

    The key (for me) to solving this was to make sure that the $table->dropForeign() command was being passed the right relationship name, not necessarily the column name. You do not want to pass the column name, as would be much more intuitive IMHO.

    What worked for me was:

    $table->dropForeign('local_table_foreign_id_foreign');
    $table->column('foreign_id');
    

    So the string I passed to dropForeign() that worked for me was in the format of:

    [local table]_[foreign key field]_foreign

    If you have access to a tool like Sequel Pro or Navicat, being able to visualize those will be very helpful.

    0 讨论(0)
  • 2020-12-13 06:12

    You can use this:

    $table->dropForeign(['pick_detail_id']);
    $table->dropColumn('pick_detail_id');
    

    If you take a peak at dropForeign source, it will build the foreign key index name for you if you pass the column name as an array.

    0 讨论(0)
  • 2020-12-13 06:14

    It turns out; when you create a foreign key like this:

    $table->integer('pick_detail_id')->unsigned();
    $table->foreign('pick_detail_id')->references('id')->on('pick_details');
    

    Laravel uniquely names the foreign key reference like this:

    <table_name>_<foreign_table_name>_<column_name>_foreign
    despatch_discrepancies_pick_detail_id_foreign (in my case)
    

    Therefore, when you want to drop a column with foreign key reference, you have to do it like this:

    $table->dropForeign('despatch_discrepancies_pick_detail_id_foreign');
    $table->dropColumn('pick_detail_id');
    

    Update:

    Laravel 4.2+ introduces a new naming convention:

    <table_name>_<column_name>_foreign
    
    0 讨论(0)
提交回复
热议问题