繁体   English   中英

如何正确删除 Laravel 5.3 中用户表列中的“唯一”列属性?

[英]How to correctly remove 'unique' column attribute in users table column in Laravel 5.3?

早期的教程给我留下了一个设置为唯一的“用户名”列。 我想更改它,以便我的唯一用户标识符与电子邮件相关联。

我已经完成了我发现的步骤:

  • 作曲家需要学说/dbal
  • php artisan make:migration modify_users_table --table=users

我的迁移包含:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('username')->unique(false)->change();
    });
}

但是,当我运行命令“php artisan migrate”时,我得到

[照明\\数据库\\查询异常]
SQLSTATE[42000]:语法错误或访问冲突:1280 不正确的索引名称 ''(SQL:alter table users add unique (us
人名))

[Doctrine\\DBAL\\Driver\\PDOException]
SQLSTATE[42000]:语法错误或访问冲突:1280 不正确的索引名称“”

[PDO 异常]
SQLSTATE[42000]:语法错误或访问冲突:1280 不正确的索引名称“”


从我最初的研究来看,问题似乎出在我的索引上:

users_username_unique

所以我的问题是:

删除该索引然后运行迁移是否安全?
如果这是正确的方法,我的迁移是否需要像这样:

$table->dropUnique('users_username_unique');

那么这个命令会自动创建一个正确的、非唯一的索引吗?

$table->string('username')->unique(false)->change();

对于 dropUnique() 方法,您必须创建一个新的迁移,

 Schema::table('users', function (Blueprint $table) {
         $table->dropUnique('username');  
 });

或者如果需要,您可以为用户名添加唯一索引。

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

删除唯一索引并添加普通索引。

$table->dropUnique(['username']);
$table->index('username');  

如果需要,为电子邮件添加唯一索引。

$table->unique('email');

数据库:迁移

您需要创建一个新的迁移并使用dropUnique()方法:

Schema::table('users', function (Blueprint $table) {
    $table->dropUnique('users_username_unique');
});

然后运行composer duphp artisan migrate命令来执行迁移。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM