Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Order Table issue with migration #90

Open
rossi99 opened this issue Jul 10, 2020 · 1 comment
Open

Create Order Table issue with migration #90

rossi99 opened this issue Jul 10, 2020 · 1 comment

Comments

@rossi99
Copy link

rossi99 commented Jul 10, 2020

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?

@GuerfiHamza
Copy link

hey, check your users table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants