[英]How to correctly remove 'unique' column attribute in users table column in Laravel 5.3?
Following tutorials early on left me with a 'username' column that is set to unique.早期的教程给我留下了一个设置为唯一的“用户名”列。 I would like to change that, so that my unique user identifier is tied to the email.
我想更改它,以便我的唯一用户标识符与电子邮件相关联。
I've done the steps I found:我已经完成了我发现的步骤:
My migration contains:我的迁移包含:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('username')->unique(false)->change();
});
}
However, when I run the command 'php artisan migrate' I get但是,当我运行命令“php artisan migrate”时,我得到
[Illuminate\\Database\\QueryException] [照明\\数据库\\查询异常]
SQLSTATE[42000]: Syntax error or access violation: 1280 Incorrect index name '' (SQL: alter table users add unique (us SQLSTATE[42000]:语法错误或访问冲突:1280 不正确的索引名称 ''(SQL:alter table users add unique (us
ername))人名))
[Doctrine\\DBAL\\Driver\\PDOException] [Doctrine\\DBAL\\Driver\\PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1280 Incorrect index name '' SQLSTATE[42000]:语法错误或访问冲突:1280 不正确的索引名称“”
[PDOException] [PDO 异常]
SQLSTATE[42000]: Syntax error or access violation: 1280 Incorrect index name '' SQLSTATE[42000]:语法错误或访问冲突:1280 不正确的索引名称“”
From my initial research it appears that the issue is with my index:从我最初的研究来看,问题似乎出在我的索引上:
users_username_unique
So my question is:所以我的问题是:
Is it safe to remove that index, then run the migration?删除该索引然后运行迁移是否安全?
If that is the correct way to do it, does my migration need to have something like this:如果这是正确的方法,我的迁移是否需要像这样:
$table->dropUnique('users_username_unique');
Then will this command automatically create a correct, non-unique, index?那么这个命令会自动创建一个正确的、非唯一的索引吗?
$table->string('username')->unique(false)->change();
For dropUnique() method, You have to create a new migration,对于 dropUnique() 方法,您必须创建一个新的迁移,
Schema::table('users', function (Blueprint $table) {
$table->dropUnique('username');
});
or If need, you can add a unique index for username.或者如果需要,您可以为用户名添加唯一索引。
Schema::table('users', function (Blueprint $table) {
$table->unique('username');
});
Drop a unique index and add a plain index.删除唯一索引并添加普通索引。
$table->dropUnique(['username']);
$table->index('username');
If need, add a unique index for email.如果需要,为电子邮件添加唯一索引。
$table->unique('email');
You need to create a new migration and use dropUnique()
method:您需要创建一个新的迁移并使用
dropUnique()
方法:
Schema::table('users', function (Blueprint $table) {
$table->dropUnique('users_username_unique');
});
Then run composer du
and php artisan migrate
commands to execute the migration.然后运行
composer du
和php artisan migrate
命令来执行迁移。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.