php 删除唯一索引 Laravel 5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36109383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drop Unique Index Laravel 5
提问by cyber8200
I kept getting this while run php artisan migrate
我在跑步时一直得到这个 php artisan migrate
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'email'; check that column/key exists
SQLSTATE[42000]:语法错误或访问冲突:1091 无法删除“电子邮件”;检查列/键是否存在
While I see that email is exist on my database.
虽然我看到我的数据库中存在电子邮件。
My migration script. I was trying to drop the unique constraint.
我的迁移脚本。我试图删除唯一约束。
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterGuestsTable3 extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('guests', function(Blueprint $table)
{
$table->dropUnique('email');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('guests', function(Blueprint $table)
{
$table->dropUnique('email');
});
}
}
Did I forget to clear any caches ?
我忘记清除任何缓存了吗?
Any hints for me ?
对我有什么提示吗?
回答by num8er
By official documentationYou can see following:
通过官方文档可以看到以下内容:
If you pass an array of columns into a method that drops indexes, the conventional index name will be generated based on the table name, columns and key type:
Schema::table('geo', function ($table) { $table->dropIndex(['state']); // Drops index 'geo_state_index' });
如果将列数组传递给删除索引的方法,则将根据表名、列和键类型生成常规索引名称:
Schema::table('geo', function ($table) { $table->dropIndex(['state']); // Drops index 'geo_state_index' });
You can drop it just simply using []
around field name:
您可以简单地使用[]
围绕字段名称来删除它:
Schema::table('guests', function(Blueprint $table)
{
$table->dropUnique(['email']);
});
UPD: By the latest docs for 7.xit's still relevant.
回答by stratedge
When dropping indexes, Laravel will expect that the full name of the index be given.
当删除索引时,Laravel 会期望给出索引的全名。
You can check your database for the full name of the index, but if the key was generated by a previous Laravel migration, its name should conform to a single, simple naming convention.
您可以检查您的数据库以获取索引的全名,但如果密钥是由先前的 Laravel 迁移生成的,则其名称应符合单一、简单的命名约定。
Here is what the documentationhas to say about its naming convention (as of v5.2):
以下是文档对其命名约定的说明(从 v5.2 开始):
By default, Laravel automatically assigns a reasonable name to the indexes. Simply concatenate the table name, the name of the indexed column, and the index type.
默认情况下,Laravel 会自动为索引分配一个合理的名称。只需连接表名、索引列的名称和索引类型。
My guess is this is why you are getting an error. There is no email
index, but there probably is a guests_email_unique
index.
我的猜测是这就是您收到错误的原因。没有email
索引,但可能有guests_email_unique
索引。
Give this migration a shot:
试一试这个迁移:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterGuestsTable3 extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('guests', function(Blueprint $table)
{
$table->dropUnique('guests_email_unique');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('guests', function(Blueprint $table)
{
//Put the index back when the migration is rolled back
$table->unique('email');
});
}
}
I understand it is a little confusing that when creating an index you specify the column names, but when dropping the index later you need to supply the index's full name.
我知道创建索引时指定列名有点令人困惑,但稍后删除索引时需要提供索引的全名。
Please note that I've adjusted the down()
method as well, so that it reverts dropping the unique index by adding it back.
请注意,我也调整了该down()
方法,以便通过将其添加回来来恢复删除唯一索引。