Php 删除带有外键的列Laravel错误:常规错误:1025重命名错误

Php 删除带有外键的列Laravel错误:常规错误:1025重命名错误,php,laravel,laravel-4,database-migration,Php,Laravel,Laravel 4,Database Migration,我使用如下迁移创建了一个表: public function up() { Schema::create('despatch_discrepancies', function($table) { $table->increments('id')->unsigned(); $table->integer('pick_id')->unsigned(); $table->foreign('pick_id')->

我使用如下迁移创建了一个表:

public function up()
{
    Schema::create('despatch_discrepancies',  function($table) {
        $table->increments('id')->unsigned();
        $table->integer('pick_id')->unsigned();
        $table->foreign('pick_id')->references('id')->on('picks');
        $table->integer('pick_detail_id')->unsigned();
        $table->foreign('pick_detail_id')->references('id')->on('pick_details');
        $table->integer('original_qty')->unsigned();
        $table->integer('shipped_qty')->unsigned();
    });
}

public function down()
{
    Schema::drop('despatch_discrepancies');
}
public function up()
{
    Schema::table('despatch_discrepancies', function($table)
    {
        $table->dropForeign('pick_detail_id');
        $table->dropColumn('pick_detail_id');
        $table->string('sku', 20)->after('pick_id');
    });
}

public function down()
{
    Schema::table('despatch_discrepancies', function($table)
    {
        $table->integer('pick_detail_id')->unsigned();
        $table->foreign('pick_detail_id')->references('id')->on('pick_details');
        $table->dropColumn('sku');
    });
}
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
<table_name>_<foreign_table_name>_<column_name>_foreign
despatch_discrepancies_pick_detail_id_foreign (in my case)
$table->dropForeign('despatch_discrepancies_pick_detail_id_foreign');
$table->dropColumn('pick_detail_id');
我需要更改此表并删除外键引用&列
pick\u detail\u id
并在
pick\u id
列之后添加一个名为
sku
的新varchar列

因此,我创建了另一个迁移,如下所示:

public function up()
{
    Schema::create('despatch_discrepancies',  function($table) {
        $table->increments('id')->unsigned();
        $table->integer('pick_id')->unsigned();
        $table->foreign('pick_id')->references('id')->on('picks');
        $table->integer('pick_detail_id')->unsigned();
        $table->foreign('pick_detail_id')->references('id')->on('pick_details');
        $table->integer('original_qty')->unsigned();
        $table->integer('shipped_qty')->unsigned();
    });
}

public function down()
{
    Schema::drop('despatch_discrepancies');
}
public function up()
{
    Schema::table('despatch_discrepancies', function($table)
    {
        $table->dropForeign('pick_detail_id');
        $table->dropColumn('pick_detail_id');
        $table->string('sku', 20)->after('pick_id');
    });
}

public function down()
{
    Schema::table('despatch_discrepancies', function($table)
    {
        $table->integer('pick_detail_id')->unsigned();
        $table->foreign('pick_detail_id')->references('id')->on('pick_details');
        $table->dropColumn('sku');
    });
}
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
<table_name>_<foreign_table_name>_<column_name>_foreign
despatch_discrepancies_pick_detail_id_foreign (in my case)
$table->dropForeign('despatch_discrepancies_pick_detail_id_foreign');
$table->dropColumn('pick_detail_id');
运行此迁移时,出现以下错误:

[照亮\数据库\查询异常]
SQLSTATE[HY000]:常规错误:重命名时发生1025错误 “./dev_iwms_重新启动/发送至” “./dev_iwms_reboot/#sql2-67c-17c464”(错误号:152)(SQL:alter table
dispatch\u差异
drop外键拾取\u细节\u id)

[PDO例外]
SQLSTATE[HY000]:常规错误:重命名时发生1025错误 “./dev_iwms_重新启动/发送至” “./dev_iwms_reboot/#sql2-67c-17c464”(错误号:152)

当我试图通过运行
php-artisan-migrate:rollback
命令来反转此迁移时,我得到一条
rollback
消息,但它实际上没有在数据库中执行任何操作


你知道哪里不对吗?如何删除包含外键引用的列;当您创建这样的外键时:

public function up()
{
    Schema::create('despatch_discrepancies',  function($table) {
        $table->increments('id')->unsigned();
        $table->integer('pick_id')->unsigned();
        $table->foreign('pick_id')->references('id')->on('picks');
        $table->integer('pick_detail_id')->unsigned();
        $table->foreign('pick_detail_id')->references('id')->on('pick_details');
        $table->integer('original_qty')->unsigned();
        $table->integer('shipped_qty')->unsigned();
    });
}

public function down()
{
    Schema::drop('despatch_discrepancies');
}
public function up()
{
    Schema::table('despatch_discrepancies', function($table)
    {
        $table->dropForeign('pick_detail_id');
        $table->dropColumn('pick_detail_id');
        $table->string('sku', 20)->after('pick_id');
    });
}

public function down()
{
    Schema::table('despatch_discrepancies', function($table)
    {
        $table->integer('pick_detail_id')->unsigned();
        $table->foreign('pick_detail_id')->references('id')->on('pick_details');
        $table->dropColumn('sku');
    });
}
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
<table_name>_<foreign_table_name>_<column_name>_foreign
despatch_discrepancies_pick_detail_id_foreign (in my case)
$table->dropForeign('despatch_discrepancies_pick_detail_id_foreign');
$table->dropColumn('pick_detail_id');
Laravel唯一地命名外键引用,如下所示:

public function up()
{
    Schema::create('despatch_discrepancies',  function($table) {
        $table->increments('id')->unsigned();
        $table->integer('pick_id')->unsigned();
        $table->foreign('pick_id')->references('id')->on('picks');
        $table->integer('pick_detail_id')->unsigned();
        $table->foreign('pick_detail_id')->references('id')->on('pick_details');
        $table->integer('original_qty')->unsigned();
        $table->integer('shipped_qty')->unsigned();
    });
}

public function down()
{
    Schema::drop('despatch_discrepancies');
}
public function up()
{
    Schema::table('despatch_discrepancies', function($table)
    {
        $table->dropForeign('pick_detail_id');
        $table->dropColumn('pick_detail_id');
        $table->string('sku', 20)->after('pick_id');
    });
}

public function down()
{
    Schema::table('despatch_discrepancies', function($table)
    {
        $table->integer('pick_detail_id')->unsigned();
        $table->foreign('pick_detail_id')->references('id')->on('pick_details');
        $table->dropColumn('sku');
    });
}
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
<table_name>_<foreign_table_name>_<column_name>_foreign
despatch_discrepancies_pick_detail_id_foreign (in my case)
$table->dropForeign('despatch_discrepancies_pick_detail_id_foreign');
$table->dropColumn('pick_detail_id');
更新: Laravel 4.2+引入了新的命名约定:

<table_name>_<column_name>_foreign
\u外国
您可以使用以下功能:

$table->dropForeign(['pick_detail_id']);
$table->dropColumn('pick_detail_id');
如果在dropForeign source上取得峰值,则如果将列名作为数组传递,它将为您构建外键索引名。

解决此问题的关键(对我而言)是确保$table->dropForeign()命令传递的是正确的关系名,而不一定是列名。您不想传递列名,因为这会更直观

对我起作用的是:

$table->dropForeign('local_table_foreign_id_foreign');
$table->column('foreign_id');
因此,我传递给dropForeign()的字符串的格式为:

[本地表].[外键字段]\u外键


如果您可以访问Sequel Pro或Navicat之类的工具,能够可视化这些工具将非常有帮助。

我想到的是,我不知道将
Schema::table
块放在哪里

后来我发现密钥位于SQL错误上:

[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists `lu_benefits_categories`)
因此,
Schema::table
块需要进入
lu\u benefits\u类别的
down()
函数中,在
Schema::dropIfExists
行之前:

public function down()
{
    Schema::table('table', function (Blueprint $table) {
        $table->dropForeign('table_category_id_foreign');
        $table->dropColumn('category_id');
    });
    Schema::dropIfExists('lu_benefits_categories');
}

之后,php artisan migrate:refresh
或php artisan migrate:reset将完成此任务。

我的表中有多个外键,然后我必须通过在down方法中将列名作为数组的索引来逐个删除外键约束:

public function up()
{
    Schema::table('offices', function (Blueprint $table) {
        $table->unsignedInteger('country_id')->nullable();
        $table->foreign('country_id')
            ->references('id')
            ->on('countries')
            ->onDelete('cascade');

        $table->unsignedInteger('stateprovince_id')->nullable();
        $table->foreign('stateprovince_id')
            ->references('id')
            ->on('stateprovince')
            ->onDelete('cascade');
        $table->unsignedInteger('city_id')->nullable();
        $table->foreign('city_id')
            ->references('id')
            ->on('cities')
            ->onDelete('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('offices', function (Blueprint $table) {
        $table->dropForeign(['country_id']);
        $table->dropForeign(['stateprovince_id']);
        $table->dropForeign(['city_id']);
        $table->dropColumn(['country_id','stateprovince_id','city_id']);
    });
} 
使用下面的语句不起作用

$table->dropForeign(['country_id','stateprovince_id','city_id']); 

因为DROPFIEW不考虑它们分离的列,我们想移除它们。因此,我们必须逐个删除它们。

在Laravel 4.2中不起作用。不是密钥名称的一部分。它只适用于uu-foreign。我在laravel 4.2中使用了它,现在仍然适用于我。
\uu-foreign
约定似乎仍然适用于5.1,在删除关系约束后,您也必须删除列。我认为文档中也应该包括这一点,因为人们可以很容易地假设dropForeign也会删除该列。谢谢分享。如果有人想知道,MySQL自动为外键创建的索引会在列被删除时被删除。无需使用
$table->dropIndex('column_name')
手动删除它们。接受的答案也很有效:您必须使用正确的索引名约定。但是这个答案也有一个问题:你必须记住索引的命名方案,而这个解决方案是自动完成的!我总是用另一种方式,总是抱怨这是多么不切实际。现在我马上切换到这个解决方案。非常感谢你!真棒的把戏。我已经像个傻瓜一样做了很久了。拉威尔真的需要一些医生的帮助。我可能会接受挑战…在Laravel 5.0中为我工作。非常感谢,亚历克斯!在Laravel 5.2中表现得很有魅力。这是一个巧妙的技巧。比记住外键命名约定(将来可能会改变)更友好。就像@ronin1184所说的,它在Laravel 5.2中工作得非常好。这很好,我只是发现它不如@Alex建议的那样用括号括起来直观。谢谢我的朋友,在数组中添加列名对我来说很有效。如果有人想知道,MySQL自动为外键创建的索引会在列被删除时被删除。无需使用
$table->dropIndex('column_name')
.ty手动删除它们,我希望
Schema::table('offices',function(Blueprint$table){}
如此糟糕。。。