SQLSTATE[42S21]: 列已存在: 1060 列名重复 'id'

SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id'

尝试迁移后出现错误。 其他页面我也没有做任何改动。

每 tables 创建为默认迁移但它没有创建我的文章 table。

这是我在CMD中遇到的错误: SQLSTATE[42S21]: 列已存在: 1060 列名重复 'id'

这是我的 _create_articles_table.php 页面:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->increments('id');
            $table->timestamps();
            $table->string('title');
            $table->text('body');
            $table->boolean('status');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

这是我的 AppServiceProvider 页面:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Schema;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        
    }

}


感谢您的宝贵时间。

显然,您定义了两次 id 列,无需添加此行:

$table->increments('id');

这将使它默认自动递增:

$table->id();

CMD: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id '

这意味着您的 id 字段在迁移查询中定义了两次,因此请在您的 articles.

中删除此行
$table->increments('id');

然后迁移你的 table 它是 运行。