删除带有外键Laravel的列错误:常规错误:1025重命名错误 [英] Dropping column with foreign key Laravel error: General error: 1025 Error on rename

查看:118
本文介绍了删除带有外键Laravel的列错误:常规错误:1025重命名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这样的迁移创建了一个表:

I've created a table using migration like this:

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');
}

我需要更改此表并删除外键引用& pick_detail_id列,并在pick_id列之后添加一个新的varchar列,称为sku.

I need to change this table and drop the foreign key reference & column pick_detail_id and add a new varchar column called sku after pick_id column.

因此,我创建了另一个迁移,看起来像这样:

So, I've created another migration, which looks like this:

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');
    });
}

运行此迁移时,出现以下错误:

When I run this migration, I get the following error:

[Illuminate \ Database \ QueryException]
SQLSTATE [HY000]:常规错误:1025重命名错误 './dev_iwms_reboot/despatch_discrepancies'到 './dev_iwms_reboot/#sql2-67c-17c464'(errno:152)(SQL:修改表 despatch_discrepancies删除外键pick_detail_id)

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './dev_iwms_reboot/despatch_discrepancies' to './dev_iwms_reboot/#sql2-67c-17c464' (errno: 152) (SQL: alter table despatch_discrepancies drop foreign key pick_detail_id)

[PDOException]
SQLSTATE [HY000]:常规错误:1025重命名错误 './dev_iwms_reboot/despatch_discrepancies'到 './dev_iwms_reboot/#sql2-67c-17c464'(errno:152)

[PDOException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './dev_iwms_reboot/despatch_discrepancies' to './dev_iwms_reboot/#sql2-67c-17c464' (errno: 152)

当我尝试通过运行php artisan migrate:rollback命令撤消此迁移时,收到一条Rolled back消息,但实际上它并没有在数据库中做任何事情.

When I try to reverse this migration by running php artisan migrate:rollback command, I get a Rolled back message, but it's not actually doing anything in the database.

任何主意可能有什么问题吗?如何删除具有外键引用的列?

Any idea what might be wrong? How do you drop a column that has a foreign key reference?

推荐答案

事实证明;当您创建像这样的外键时:

It turns out; when you create a foreign key like this:

$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');

Laravel唯一地命名外键引用,如下所示:

Laravel uniquely names the foreign key reference like this:

<table_name>_<foreign_table_name>_<column_name>_foreign
despatch_discrepancies_pick_detail_id_foreign (in my case)

因此,当您要删除带有外键引用的列时,必须这样做:

Therefore, when you want to drop a column with foreign key reference, you have to do it like this:

$table->dropForeign('despatch_discrepancies_pick_detail_id_foreign');
$table->dropColumn('pick_detail_id');

更新:

Laravel 4.2+引入了新的命名约定:

Update:

Laravel 4.2+ introduces a new naming convention:

<table_name>_<column_name>_foreign

这篇关于删除带有外键Laravel的列错误:常规错误:1025重命名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆