【问题标题】:Laravel Column already exists: 1060 Duplicate column name 'cm_firebase_token'Laravel Column 已经存在:1060 Duplicate column name 'cm_firebase_token'
【发布时间】:2021-11-03 08:15:19
【问题描述】:

我正在尝试编辑现有项目,我添加了一些 php artisan migrations,当我尝试 php artisan migratephp artisan schema:dump 时,它一直显示此错误消息,我尝试了 composer updatecomposer require doctrine/dbal 以及我的 @ 987654326@看起来像这样

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:/NC6CNBiDJb2vV4fRviEsMqy5gKbePRgk44JGkZFAYY=
APP_DEBUG=true
APP_URL=http://localhost
APP_MODE=dev

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=phpmyadmin
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
SOFTWARE_ID=MzM1NzE3NTA=
BUYER_USERNAME=tt
PURCHASE_CODE=fsgf

在终端中显示这样的错误请任何人建议我解决这个问题

Illuminate\Database\QueryException

SQLSTATE[42S21]:  Column already exists: 1060 Duplicate column name 'cm_firebase_token'(SQL: alter table `users` add `cm_firebase_token` varchar(191) null)

  at C:\Users\Crazymhegde\Downloads\Admin Install\vendor\laravel\framework\src\Illuminate\Database\Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕     }

  1   C:\Users\Crazymhegde\Downloads\Admin Install\vendor\laravel\framework\src\Illuminate\Database\Connection.php:485
      PDOException::("SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'cm_firebase_token'")

  2   C:\Users\Crazymhegde\Downloads\Admin Install\vendor\laravel\framework\src\Illuminate\Database\Connection.php:485
      PDOStatement::execute()

这个错误显示的代码在这里我添加了它所需的一切

<?php

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

class AddCmFirebaseTokenColumnToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('cm_firebase_token')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('cm_firebase_token');
        });
    }
}

【问题讨论】:

  • 错误告诉你:你尝试创建的列已经存在
  • 如果我删除或删除此迁移是否可以正常工作/有任何问题
  • 您之前可能已经运行过迁移并且没有执行 php artisan migrate:rollback 来撤消更改,尽管迁移表现在似乎不同步,所以请手动删除该列

标签: php mysql laravel laravel-8


【解决方案1】:

列名cm_firebase_token 已经存在于表中。看起来您是在手动将列添加到表中后创建了迁移。

只需复制迁移文件名并将其插入到migrations 表中即可。

【讨论】:

  • 在迁移表中手动输入并不好,就像手动添加列一样。最好回滚迁移。
  • 是否可以回滚migrations 表中不存在的迁移?
【解决方案2】:

你应该使用 php artisan migrate:fresh 命令来更新你的数据库

【讨论】:

  • 这是评论,不是答案
猜你喜欢
  • 2020-02-27
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多