首页 > 解决方案 > Laravel Column 已经存在:1060 Duplicate column name

问题描述

我正在尝试进行此迁移,但它给了我这个错误

enter code here
<?php

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

class CreateUsersTable extends Migration
{

public function up()
{

    //CREANDO UN MODELO PARA LOS ROLES DEL USUARIO
    Schema::create('roles', function (Blueprint $table) {
        $table->Increments('id');
        $table->string('name')->comment('Nombre del rol del usuario');
        $table->text('description');
        $table->timestamps();
    });

    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('role_id')->default(\App\Role::STUDENT);
        $table->foreign('role_id')->references('id')-> on('roles');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->string('picture')->nullable();

        //cashier columns
        $table->string('stripe_id')->nullable();
        $table->string('card_brand')->nullable();
        $table->string('card_last_four', 4)->nullable();
        $table->timestamp('trial_ends_at')->nullable();


        $table->rememberToken();
        $table->timestamps();
    });

    Schema::create('subscriptions',function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');
            $table->string('name');
            $table->string('stripe_status');
            $table->string('stripe_plan');
            $table->integer('quantity');
            $table->timestamp('trial_ends_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->timestamps();
        });

    Schema::create('user_social_accounts', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')-> on('users');
        $table->string('provider');
        $table->string('provider_uid');

    });
    }


    public function down()
    {
    Schema::dropIfExists('users');
    Schema::dropIfExists('roles');
    Schema::dropIfExists('subscriptions');
    Schema::dropIfExists('user_social_accounts');
    }
    }

并给我这个错误

列已存在:1060 列名重复“stripe_id”(SQL:alter table usersadd stripe_idvarchar(255) null,add card_brandvarchar(255) null,add card_last_fourvarchar(4) null,add trial_ends_attimestamp null)

异常跟踪:

1 PDOException::("SQLSTATE[42S21]: 列已经存在:1060 列名重复'stripe_id'") C:\laragon\www\learning\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459

2 PDOStatement::execute() C:\laragon\www\learning\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459

标签: laravel

解决方案


Cashier 带有迁移功能,可以为您将“客户列”添加到users表中。

“Cashier 服务提供商注册了自己的数据库迁移目录,因此请记住在安装软件包后迁移您的数据库。Cashier 迁移会在您的用户表中添加几列,并创建一个新的订阅表来保存您所有客户的订阅”

Laravel 6.x 文档 - 收银员 - 安装

因此,这些迁移正在尝试添加您自己添加的列。

“如果您想阻止 Cashier 的迁移完全运行,您可以使用ignoreMigrationsCashier 提供的。”


推荐阅读