php - SQLSTATE [HY000]:一般错误:1005 无法创建表 `auth`.`order_items`
问题描述
SQLSTATE[HY000]:一般错误:1005 无法创建表auth
。order_items
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->integer('category_id')->unsigned();
$table->string('image')->default('default.png');
$table->double('purchase_price', 8, 2);
$table->double('sale_price', 8, 2);
$table->integer('stock');
$table->timestamps();
// $table->userstamps();
// $table->softUserstamps();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('order_number');
$table->unsignedBigInteger('user_id');
$table->enum('status', ['pending','processing','completed','decline'])->default('pending');
$table->float('grand_total');
$table->integer('item_count');
$table->boolean('is_paid')->default(false);
$table->enum('payment_method', ['cash_on_delivery', 'paypal','stripe','card'])->default('cash_on_delivery');
$table->string('shipping_fullname');
$table->string('shipping_address');
$table->string('shipping_city');
$table->string('shipping_state');
$table->string('shipping_zipcode');
$table->string('shipping_phone');
$table->string('notes')->nullable();
$table->string('billing_fullname');
$table->string('billing_address');
$table->string('billing_city');
$table->string('billing_state');
$table->string('billing_zipcode');
$table->string('billing_phone');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrderItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('order_items', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('order_id');
$table->unsignedBigInteger('product_id');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->float('price');
$table->integer('quantity');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('order_items');
}
}
解决方案
product_id
您正在使用在您的order_items
表 中创建外键,$table->unsignedBigInteger('product_id')
但它是您的产品表中定义的整数,$table->increments('id');
这就是您收到此错误的原因。
创建foreign key
child column
数据类型必须与数据类型parent column
相同
要解决此错误更改unsignedBigInteger
为unsignedInteger
推荐阅读
- mysql - 使用多对多关系表查找值
- java - Struts 1.3 中的过滤器列表
- jenkins - 擦除 Jenkins 环境变量
- javascript - 是否有可能在 Firebase 上为移动应用创建帐户并让用户使用预设凭据登录?
- ios - 如何使用 NSFetchRequest 和 fetchLimit = 1 从核心数据中获取 0 或 1 个实体?
- php - 如何在我保存的 laravel 中获得无限级别的菜单
- python - 如何在 django 中创建没有应用程序的模型?
- c# - MoveTo 在本地实例上工作,Azure 中的身份验证错误
- python - 我在 python 中编写了一个动画代码,但它给出了 modulenotfounderror 。没有名为 vector 的模块。能不能解释一下原因。请
- oracle - 通过来自 oracle 数据库的 post 请求发送大量数据