我在尝试运行时收到此错误: 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');
}
}
尝试运行时:作曲家转储自动加载 - 它只返回以下内容:
Generating autoload files
老实说,我无法确定重复出现在哪里。任何帮助都会很可爱。
谢谢。
我已经将错误(在终端中)中提到的表的 down 方法更改为:
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
Schema::dropIfExists('follower_followee');
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
}
有了这个,我可以删除父表而不会出现外键错误。
然后手动从数据库中删除我的所有表,然后运行 PHP Artisan migrate 和 PHP Artisan Migrate:刷新,没有任何错误。感谢试图帮助的人!
您正在创建列"follower_id"和"followee_id"两次:
$table->integer('follower_id')->unsigned();
$table->foreign('follower_id')->references('id')->on('users')->onDelete('cascade');
在这两种情况下,第一个语句都是多余的,并导致上述错误。
- 编辑:阅读文档我意识到是错误的,对不起。
OP对这个问题的自我回答实际上不是正确的方法。您应该删除已建立的外键关系,以免遇到此错误:
public function down(){
Schema::table('follower_followee', function (Blueprint $table) {
$table->dropForeign('followee_id_users_foreign');
$table->dropForeign('follower_id_users_foreign');
});
}
如果外来名称不正确,您可以在 PhpMyAdmin(如果使用)中的表>结构>关系下找到正确的名称