如何在Laravel迁移中修复外键错误 [英] how to fix foreign key error in laravel migration

查看:107
本文介绍了如何在Laravel迁移中修复外键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于订单的订单表,我之前创建过该表,但是有时我必须更改迁移.

I have a order table for my orders, i created this table before, but after some times i have to change my 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']);
    });

这是更改后的 orders表:

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行:

as you can see i import 3 new lines in orders schema :

$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 migration 时,出现此错误:

But when i want to run php artisan migrate i get this error :

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

然后在地址之前迁移订单.

And order migrate before address.

推荐答案

我希望您已使用迁移来更改订单表,而不仅仅是修改了原始迁移?

i hope you have used a migration to change the order table and not just modified the original migration?

此错误通常是出于以下几个原因造成的:

this error generally happens for few reasons:

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

这篇关于如何在Laravel迁移中修复外键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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