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.
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.
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!
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