如何修复 laravel 迁移中的外键错误

how to fix foreign key error in laravel migration

我有一个订单 table,我之前创建了这个 table,但有时我不得不更改迁移。

这是我的 订单 table 更改前:

Schema::create('orders', function (Blueprint $table) {
        $table->id();

        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        $table->bigInteger('price');
        $table->enum('status', ['unpaid', 'paid', 'preparation', 'posted', 'recieved', 'canceled']);
        $table->string('tracking_serial')->nullable();

        $table->timestamps();
    });

    Schema::create('order_product', function (Blueprint $table) {
        $table->unsignedBigInteger('product_id');
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

        $table->unsignedBigInteger('order_id');
        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');

        $table->integer('quantity');

        $table->primary(['product_id', 'order_id']);
    });

这是订单table修改后的:

Schema::create('orders', function (Blueprint $table) {
        $table->id();

        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        $table->unsignedBigInteger('address_id');
        $table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');

        $table->bigInteger('price');
        $table->string('post_type');
        $table->enum('status', ['unpaid', 'paid', 'preparation', 'posted', 'recieved', 'canceled']);
        $table->string('tracking_serial')->nullable();

        $table->primary(['user_id', 'address_id']);

        $table->timestamps();
    });

    Schema::create('order_product', function (Blueprint $table) {
        $table->unsignedBigInteger('product_id');
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

        $table->unsignedBigInteger('order_id');
        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');

        $table->integer('quantity');

        $table->primary(['product_id', 'order_id']);
    });

如您所见,我在 orders 模式中导入了 3 行新行:

$table->unsignedBigInteger('address_id');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');

$table->string('post_type');

$table->primary(['user_id', 'address_id']);

但是当我想要 运行 php artisan migrate 时,我得到了这个错误:

SQLSTATE[HY000]: General error: 1005 Can't create table `shop`.`orders` (errno: 150 "Foreign key 
constraint is incorrectly formed") (SQL: alter table `orders` add constraint 
`orders_address_id_foreign` foreign key (`address_id`) references `addresses` (`id`) on delete 
cascade)

为什么我会出现这个错误?

更新:

这是我的地址 table :

Schema::create('addresses', function (Blueprint $table) {
        $table->id();
        $table->string('state');
        $table->string('city');
        $table->text('address');
        $table->integer('plaque');
        $table->string('postal');
        $table->timestamps();
    });

    Schema::create('address_user', function (Blueprint $table) {
        $table->id();

        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')
        ->onDelete('cascade');
        $table->unsignedBigInteger('address_id');
        $table->foreign('address_id')->references('id')->on('addresses')
        ->onDelete('cascade');
        $table->primary(['user_id', 'address_id']);

        $table->timestamps();
    });

并在地址之前迁移订单。

我希望您使用迁移来更改顺序 table 而不是仅仅修改了原始迁移?

此错误的发生通常有以下几个原因:

  1. fk字段(address_id)和pk字段(即地址)不是同一类型
  2. 订单的迁移是 运行 在地址 table 之前(我认为在这种情况下不太可能,因为错误会有所不同)
  3. address_id 不可为空(据我所知)因此当您创建 FK 时,当前存在的行将没有有效的 FK 来寻址。 (所以让它可以为空)

您必须先创建 table,然后再创建外键:

Schema::create('orders', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id');
        $table->unsignedBigInteger('address_id');
        $table->bigInteger('price');
        $table->string('post_type');
        $table->enum('status',['unpaid','paid','preparation','posted', 'recieved', 'canceled']);
        $table->string('tracking_serial')->nullable();
        $table->primary(['user_id', 'address_id']);
        $table->timestamps();
    });


Schema::table('orders', function (Blueprint $table) {

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');
    });