删除外键约束并新增列

后端开发 2026-07-10

在删除外键约束相关的列并添加新列时遇到错误。错误信息显示该表中不存在该列。该如何修复?

Syntax error or access violation: 1091 Can't DROP 'pallet_distributions_rental_purchase_order_id_foreign'; check that column/key exists (SQL: alter table pallet\_distributions drop foreign key pallet\_distributions\_rental\_purchase\_order\_id\_foreign)

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddOriginAndDestinationRentalPurchaseOrderIdFieldsToPalletDistributionsTable extends Migration
{
    public function up()
    {
        Schema::table('pallet_distributions', function (Blueprint $table) {
            $table->dropForeign(['rental_purchase_order_id']);

            $table->dropColumn('rental_purchase_order_id');

            $table->unsignedBigInteger('origin_rental_purchase_order_id')->nullable();
            $table->unsignedBigInteger('destination_rental_purchase_order_id')->nullable();

            $table->foreign('origin_rental_purchase_order_id', 'origin_rental_purchase_order_fk_10726539')
                ->references('id')
                ->on('rental_purchase_orders');

            $table->foreign('destination_rental_purchase_order_id', 'destination_rental_purchase_order_fk_10746540')
                ->references('id')
                ->on('rental_purchase_orders');
        });
    }

    public function down()
    {
        Schema::table('pallet_distributions', function (Blueprint $table) {
            $table->dropForeign('origin_rental_purchase_order_fk_10726539');
            $table->dropForeign('destination_rental_purchase_order_fk_10746540');

            $table->dropColumn([
                'origin_rental_purchase_order_id',
                'destination_rental_purchase_order_id',
            ]);

            $table->unsignedBigInteger('rental_purchase_order_id')->nullable();

            $table->foreign('rental_purchase_order_id', 'rental_purchase_order_fk_10721477')
                ->references('id')
                ->on('rental_purchase_orders');
        });
    }
}

解决方案

外键名称不匹配。请连接一个数据库管理系统(如HeidiSQL)到你的数据库,并在模式中查找实际外键名称

pallet_distributions

表。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章