Php SQLSTATE[HY000]:一般错误:1005CAN';t创建表'auth`.'order\u items`
SQLSTATE[HY000]:一般错误:1005无法创建表Php SQLSTATE[HY000]:一般错误:1005CAN';t创建表'auth`.'order\u items`,php,laravel,Php,Laravel,SQLSTATE[HY000]:一般错误:1005无法创建表authorder\u项目 <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateProductsTable extends Migration { /** * Run
auth
order\u项目
<?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');
}
}
您正在使用$table->unsignedbiginger('product\u id')
在您的订单项目中的产品id
上创建外键,但它在您的产品表$table->increments('id')
中是definend integer代码>这就是为什么会出现此错误
要创建外键
子列
数据类型必须与父列
数据类型相同
要解决此错误,请将unsignedbiginger
更改为unsignedInteger您正在使用$table->unsignedbiginger('product_id')
在$table->increments('id')
在您的$table->increments('id')的产品表中定义了一个整数代码>这就是为什么会出现此错误
要创建外键
子列
数据类型必须与父列
数据类型相同
要解决此错误,请将unsignedbiginger
更改为unsignedInteger
错误消息是否有其他内容?categories.id
可能不是无符号整数。错误消息是否有其他内容?categories.id
可能不是无符号整数。