【发布时间】: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