laravel-ecommerce-example
laravel-ecommerce-example copied to clipboard
Create Order Table issue with migration
I am on part 18 of the series and I am having issues with the migration of the orders table and the order products table.
My '2020_07_10_134530_create_orders_table.php' look like this:
`<?php
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
class CreateOrdersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('orders', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->nullable(); $table->foreign('user_id')->references('id')->on('users') ->onUpdate('cascade')->onDelete('set null'); $table->string('billing_email')->nullable(); $table->string('billing_name')->nullable(); $table->string('billing_address')->nullable(); $table->string('billing_city')->nullable(); $table->string('billing_province')->nullable(); $table->string('billing_postalcode')->nullable(); $table->string('billing_phone')->nullable(); $table->string('billing_name_on_card')->nullable(); $table->integer('billing_discount')->default(0); $table->string('billing_discount_code')->nullable(); $table->integer('billing_subtotal'); $table->integer('billing_tax'); $table->integer('billing_total'); $table->string('payment_gateway')->default('stripe'); $table->boolean('shipped')->default(false); $table->string('error')->nullable(); $table->timestamps(); }); }
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}
And my '2020_07_10_135517_create_order_product_table.php' looks like:
`<?php
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
class CreateOrderProductTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('order_product', function (Blueprint $table) { $table->increments('id'); $table->integer('order_id')->unsigned()->nullable(); $table->foreign('order_id')->references('id') ->on('orders')->onUpdate('cascade')->onDelete('set null');
$table->integer('product_id')->unsigned()->nullable();
$table->foreign('product_id')->references('id')
->on('products')->onUpdate('cascade')->onDelete('set null');
$table->integer('quantity')->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('order_product');
}
} `
When I run:
php artisan migrate
I get the following error:
SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'orders_user_id_foreign' are incompatible. (SQL: alter table
ordersadd constraint
orders_user_id_foreign foreign key (
user_id) references
users (
id) on delete set null on update cascade)
Someone help me please?
hey, check your users table