gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:05:34 24 4
gpt4 key购买 nike

我有一个订单表,我之前创建了这个表,但有时我不得不改变我的迁移。
这是我更改前的订单表:

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();
});
并在地址之前订购迁移。

最佳答案

我希望您使用迁移来更改订单表,而不仅仅是修改原始迁移?
发生此错误的原因通常有以下几个:

  • fk 字段(address_id)和 pk 字段(即地址)不是同一类型
  • 订单迁移在地址表之前运行(我认为在这种情况下不太可能,因为错误会有所不同)
  • address_id 不可为空(据我所知),因此当您创建 FK 时,当前存在的行将没有有效的 FK 可寻址。 (所以让它可以为空)
  • 关于php - 如何修复laravel迁移中的外键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62961922/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com