[英]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();
});
并在地址之前订购迁移。
我希望您已经使用迁移来更改订单表,而不仅仅是修改了原始迁移?
此错误通常由于以下几个原因而发生:
您必须先创建表,然后创建外键:
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.