【问题标题】:Updating Existing Column Attributes - Laravel 5 Migration更新现有列属性 - Laravel 5 迁移
【发布时间】:2023-04-05 07:25:01
【问题描述】:

我有

一个名为cpe_mac 的现有列。我通过这样的迁移创建了它:

$table->string('cpe_mac')->default(NULL)->nullable();

我想要

我想将此->unique() 添加到该列,而不必删除它并重新添加


我试过了

$table->string('cpe_mac')->unique();

迁移文件

<?php

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

class AlterCaptivePortalTable212017 extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('captive_portals', function (Blueprint $table) {
            $table->string('cpe_mac')->unique();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('captive_portals', function (Blueprint $table) {
            $table->string('cpe_mac')->default(NULL)->nullable();
        });
    }
}

我保留了

得到

SQLSTATE[42701]: Duplicate column: 7 ERROR:  column "cpe_mac" of relation "captive_portals" already exists

有没有办法在不删除现有列的情况下实现这一点?我有很多无法删除的客户数据。

【问题讨论】:

    标签: php laravel laravel-5 database-migration


    【解决方案1】:

    你需要使用change()方法:

    Schema::table('captive_portals', function (Blueprint $table) {
        $table->string('cpe_mac')->unique()->change();
    });
    

    或者,您可以在定义列之后创建索引。例如:

    $table->unique('email');
    

    https://laravel.com/docs/5.4/migrations#indexes

    【讨论】:

      【解决方案2】:

      Schema::table('users', function (Blueprint $table) { $table->string('cpe_mac')->unique()->change(); });

      https://laravel.com/docs/5.0/schema#changing-columns

      【讨论】:

        【解决方案3】:

        如果该列已定义,您可以使用:

        $table->unique('cpe_mac');
        

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题,这是 Laravel 5.6 的解决方案:

          第一步:运行这个命令:composer require doctrine/dbal

          第二步:运行这个命令:php artisan make:migration THE -NAME_YOU_WANT --table=TABLENAME

          step3:在添加的migration中,在Schema::table部分添加$table-&gt;string('cpe_mac')-&gt;unique()-&gt;change();

          第四步:运行这个命令:php artisan migrate

          【讨论】:

            猜你喜欢
            • 2016-07-27
            • 2021-05-13
            • 2021-07-01
            • 2019-05-03
            • 2017-08-29
            • 2013-05-23
            • 2015-06-27
            • 2019-12-06
            • 2017-10-03
            相关资源
            最近更新 更多