【问题标题】:error with migrate refresh迁移刷新错误
【发布时间】:2016-02-28 06:57:53
【问题描述】:

尝试运行时出现此错误: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

老实说,我无法确定重复出现在哪里。任何帮助都会很可爱。

谢谢。

【问题讨论】:

  • 您似乎正在回滚所有内容,因此请尝试删除数据库中的所有内容,然后运行php artisan migrate
  • 这可能是由于一个迁移的down 方法没有实现正确的查询来回滚迁移。您确定您没有在另一个迁移中创建follower_id,但在回滚时无法将其删除,现在您正尝试使用此迁移重新创建它。在任何情况下,您都应该检查数据库并查看表的当前状态是否符合您的预期,因为您可能需要进行一些手动调整才能使迁移再次工作。
  • @PeterPan666 这就是我做了几次。当尝试更新一些迁移并运行 php artisan migrate:refresh 时,它给了我同样的错误。 @Bogdan 我已将迁移的 drop 方法更改为:public function down() { Schema::table('follower_followee', function (Blueprint $table) { Schema::dropIfExists('follower_followee'); } }
  • 现在它在尝试运行 migrate:refresh 时给了我一个新错误。它现在指向其他迁移(用户):代码很长,所以这里有一个外部链接:paste.ofcode.org/59GGkDpFbhsVrSKdVUY7Jg
  • 为什么要做dropIfExists 而不是简单的drop

标签: php laravel laravel-5 laravel-5.1


【解决方案1】:

我已将错误中提到的表格向下方法(在终端中)更改为:

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:refresh 没有任何错误。 感谢任何试图提供帮助的人!

【讨论】:

    【解决方案2】:

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

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

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

    -- 编辑:阅读我发现错误的文档,抱歉。

    【讨论】:

      【解决方案3】:

      OP 对这个问题的自我回答实际上并不是解决问题的正确方法。您应该改为删除已建立的外键关系,以免遇到此错误:

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

      如果外来的名字不正确,可以在PhpMyAdmin(如果正在使用)的table->structure->relationships下找到正确的名字

      【讨论】:

      • 不幸的是,我在运行 php artisan migrate:refresh 时遇到错误:jpst.it/FRR6
      猜你喜欢
      • 2016-07-22
      • 2016-05-17
      • 2018-03-19
      • 2017-06-27
      • 2016-03-03
      • 2017-07-24
      • 2014-12-27
      • 1970-01-01
      • 2016-05-22
      相关资源
      最近更新 更多