【问题标题】:SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id'SQLSTATE [42S21]:列已存在:1060 列名“id”重复
【发布时间】:2021-09-12 13:59:41
【问题描述】:

尝试迁移后出现错误。 并且我没有对其他页面进行任何更改。

作为迁移默认创建的每个表,但它没有创建我的文章表。

这是我在 CMD 中遇到的错误: SQLSTATE[42S21]: 列已经存在: 1060 Duplicate column name '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()
    {
        
    }

}


感谢您的宝贵时间。

【问题讨论】:

  • 执行$table-&gt;id(); 等同于$table-&gt;increments('id');,因此无需执行两次。删除其中一个

标签: php laravel migration


【解决方案1】:

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

$table->increments('id');

这将使其默认自动递增:

$table->id();

【讨论】:

    【解决方案2】:

    CMD: SQLSTATE[42S21]: 列已经存在: 1060 Duplicate column name 'id'

    这意味着您的 id 字段在迁移查询中定义了两次,因此请在您的文章中删除此行。

    $table->increments('id');
    

    然后迁移它运行的表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 2016-08-02
      • 2011-06-16
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 2016-02-02
      相关资源
      最近更新 更多