迁移刷新时出错

时间:2016-02-27 18:30:31

标签: php laravel laravel-5 laravel-5.1

我在尝试运行时收到此错误: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'

这是错误引用的迁移:

<?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');
    }
}

尝试运行时:composer dump-autoload - 它只返回:

Generating autoload files

老实说,我无法确定出现重复的位置。任何帮助都很可爱。

谢谢。

3 个答案:

答案 0 :(得分:1)

我已经改变了表格&#39;错误中提到的down方法(在终端中)到此:

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

有了这个,我可以删除没有外键错误的父表。

仅为表格做了。然后从db手动删除我的所有表,然后运行php artisan migrate和php artisan migrate:刷新没有任何错误。 感谢任何想要帮助的人!

答案 1 :(得分:0)

您正在创建列'follower_id'和'followee_id'两次:

new Double[] {...}

在这两种情况下,第一个语句都是多余的,并导致上述错误。

- 编辑:阅读文档我意识到错了,抱歉。

答案 2 :(得分:0)

OP对这个问题的自我回答实际上并不是解决问题的正确方法。你应该放弃建立的外键关系,这样你就不会遇到这个错误:

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

如果外国人的姓名不正确,您可以在表格&gt;结构 - &gt;关系

下的PhpMyAdmin(如果使用中)中找到正确的名称