簡體   English   中英

如何修復 laravel 遷移中的外鍵錯誤

[英]how to fix foreign key error in laravel migration

我有一個訂單表,我之前創建了這個表,但有時我不得不更改我的遷移。

這是更改前我的訂單表

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

這是更改后的訂單表

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)

為什么我有這個錯誤?

更新:

這是我的地址表:

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

並在地址之前訂購遷移。

我希望您已經使用遷移來更改訂單表,而不僅僅是修改了原始遷移?

此錯誤通常由於以下幾個原因而發生:

  1. fk字段(address_id)和pk字段(即address)不是同一類型
  2. 訂單遷移在地址表之前運行(我認為在這種情況下不太可能,因為錯誤會有所不同)
  3. address_id 不可為空(據我所知)因此當您創建 FK 時,當前存在的行將沒有有效的 FK 來尋址。 (所以讓它可以為空)

您必須先創建表,然后創建外鍵:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM