我必须使用Laravel迁移从电子邮件列中删除唯一约束。这是我的代码:
class AlterEmailToUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('email')->unique(false)->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->string('email')->nullable(false)->unique()->change();
});
}
}
但是当我运行php artisan migrate
时,我收到以下错误:
SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'users_email_unique' (SQL: alter table `users` add unique `users_email_unique`(`email`))
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('email')->unique(false)->nullable()->change();
});
}
改成
$table->dropUnique('users_email_unique');
我想你可以放弃你的专栏
并重新创建:
请注意,它会删除电子邮件列中的值
if (Schema::hasColumn('users', 'email'))
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('email');
});
}
Schema::table('users', function (Blueprint $table) {
$table->string('email')->nullable();
});