簡體   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