Php 如何修复laravel迁移中的外键错误
我的订单有一个订单表,我以前创建过这个表,但过了一些时候,我不得不更改我的迁移 这是更改前的“我的订单”表:Php 如何修复laravel迁移中的外键错误,php,laravel,migration,laravel-7,migrate,Php,Laravel,Migration,Laravel 7,Migrate,我的订单有一个订单表,我以前创建过这个表,但过了一些时候,我不得不更改我的迁移 这是更改前的“我的订单”表: Schema::create('orders', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('user_id'); $table->foreign('user_id')->references('id')->o
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']);
});
这是更改后的订单表:
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
schema中导入了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');
});
“地址”的迁移在哪里?问题在您的
addreses
表上,可能是您稍后创建的(在迁移orders
表之前)。或者您的主键不正确formatted@lagbox问题更新是我的订单表在地址表之前运行,我能做什么在订单表之前运行地址表,我的问题得到解决。thanks@Waltun只需跟进-您可以将地址迁移的时间戳更改为在order表之前。。