我有
名为 cpe_mac
的现有列。我通过迁移创建了它,如下所示:
$table->string('cpe_mac')->default(NULL)->nullable();
<小时 />我要
我想将此->unique()
添加到该列中,而不必删除它并重新添加。
我试过了
$table->string('cpe_mac')->unique();
<小时 />迁移文件
<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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
有没有办法在不删除现有列的情况下实现这一目标?我有很多无法删除的客户数据。
Schema::table('users', function (Blueprint $table) {
$table->string('cpe_mac')->unique()->change();
});
https://laravel.com/docs/5.0/schema#changing-columns
使用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
我遇到了同样的问题,这是Laravel 5.6的解决方案:
步骤1:运行以下命令:composer require doctrine/dbal
步骤2:运行以下命令:php artisan make:migration THE
-NAME_YOU_WANT --table=TABLENAME
step3:在添加的迁移中,在架构::表部分添加$table->string('cpe_mac')->unique()->change();
。
步骤4:运行以下命令:php artisan migrate
如果已定义列,则可以使用:
$table->unique('cpe_mac');