laravel-ecommerce-example icon indicating copy to clipboard operation
laravel-ecommerce-example copied to clipboard

Create Order Table issue with migration

Open rossi99 opened this issue 4 years ago • 1 comments

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 tableordersadd constraintorders_user_id_foreign foreign key (user_id) references users (id) on delete set null on update cascade)

Someone help me please?

rossi99 avatar Jul 10 '20 15:07 rossi99

hey, check your users table

GuerfiHamza avatar Nov 03 '20 10:11 GuerfiHamza