error with migrate refresh

倾然丶 夕夏残阳落幕 提交于 2020-01-07 03:49:25

问题


I am getting this error when trying to run: php artisan migrate:refresh:

Rolled back: 2016_02_16_114444_create_posts_table
Rolled back: 2016_01_20_234538_expectations
Rolled back: 2016_01_20_200616_expectation_profile
Rolled back: 2015_12_22_111958_create_profiles_table
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table


  [Illuminate\Database\QueryException]                                          
  SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'follower  
  _id' (SQL: alter table `follower_followee` add `follower_id` int unsigned no  
  t null, add `followee_id` int unsigned not null)                              

  [PDOException]                                                                
  SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'follower  
  _id'

This is the migration the error refers to:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class FollowerFollowee extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('follower_followee', function (Blueprint $table) 
        {
            $table->integer('follower_id')->unsigned(); // follower id number,must be positive.
            $table->integer('followee_id')->unsigned(); // followee id number,must be positive.
            $table->foreign('follower_id')->references('id')->on('users')->onDelete('cascade');
            //The 'follower_id' column references to the 'id' column in a 'users' table.
            //When a user is deleted in the parent column ('follower_id'), then also the user in 'id' ('users') is deleted. 
            $table->foreign('followee_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()

{
    Schema::dropIfExists('follower_followee');
    }
}

when trying to run : composer dump-autoload - it returns only this:

Generating autoload files

I honestly can't identify where's that duplication appears. Any help would be lovely.

Thank you.


回答1:


I have changed the tables' down method mentioned in the error (in the terminal) to this:

public function down()
{
    DB::statement('SET FOREIGN_KEY_CHECKS = 0');
    Schema::dropIfExists('follower_followee');
    DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
}

With this I can delete parent tables without errors for foreign keys.

did it for the tables only. then removed manually all my tables from db and then ran php artisan migrate and php artisan migrate:refresh without any errors. Thanks for whoever tried to help!




回答2:


You are creating column 'follower_id' and 'followee_id' twice:

$table->integer('follower_id')->unsigned();

$table->foreign('follower_id')->references('id')->on('users')->onDelete('cascade'); 

The first statement is redundant in both cases and causes the mentioned error.

-- Edit: Reading the docs I realize am wrong, sorry.




回答3:


The OP's self answer to this question is actually not the right way to go about it. You should be instead dropping the foreign key relationship established so that you don't run into this error:

public function down(){
    Schema::table('follower_followee', function (Blueprint $table) {
        $table->dropForeign('followee_id_users_foreign');
        $table->dropForeign('follower_id_users_foreign');
    });
}

If the name of the foreign is incorrect, you can find the proper name in PhpMyAdmin (if in use) under table->structure->relationships



来源:https://stackoverflow.com/questions/35673724/error-with-migrate-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!