5

I have a order table for my orders, i created this table before, but after some times i have to change my migration.

This is my orders table before changes :

Schema::create('orders', function (Blueprint $table) {
        $table->id();

        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        $table->bigInteger('price');
        $table->enum('status', ['unpaid', 'paid', 'preparation', 'posted', 'recieved', 'canceled']);
        $table->string('tracking_serial')->nullable();

        $table->timestamps();
    });

    Schema::create('order_product', function (Blueprint $table) {
        $table->unsignedBigInteger('product_id');
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

        $table->unsignedBigInteger('order_id');
        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');

        $table->integer('quantity');

        $table->primary(['product_id', 'order_id']);
    });

And this is orders table after changes :

Schema::create('orders', function (Blueprint $table) {
        $table->id();

        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        $table->unsignedBigInteger('address_id');
        $table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');

        $table->bigInteger('price');
        $table->string('post_type');
        $table->enum('status', ['unpaid', 'paid', 'preparation', 'posted', 'recieved', 'canceled']);
        $table->string('tracking_serial')->nullable();

        $table->primary(['user_id', 'address_id']);

        $table->timestamps();
    });

    Schema::create('order_product', function (Blueprint $table) {
        $table->unsignedBigInteger('product_id');
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

        $table->unsignedBigInteger('order_id');
        $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');

        $table->integer('quantity');

        $table->primary(['product_id', 'order_id']);
    });

as you can see i import 3 new lines in orders schema :

$table->unsignedBigInteger('address_id');
$table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');

$table->string('post_type');

$table->primary(['user_id', 'address_id']);

But when i want to run php artisan migrate i get this error :

SQLSTATE[HY000]: General error: 1005 Can't create table `shop`.`orders` (errno: 150 "Foreign key 
constraint is incorrectly formed") (SQL: alter table `orders` add constraint 
`orders_address_id_foreign` foreign key (`address_id`) references `addresses` (`id`) on delete 
cascade)

Why do I have this error ?

UPDATE :

This is my addresses table :

Schema::create('addresses', function (Blueprint $table) {
        $table->id();
        $table->string('state');
        $table->string('city');
        $table->text('address');
        $table->integer('plaque');
        $table->string('postal');
        $table->timestamps();
    });

    Schema::create('address_user', function (Blueprint $table) {
        $table->id();

        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')
        ->onDelete('cascade');
        $table->unsignedBigInteger('address_id');
        $table->foreign('address_id')->references('id')->on('addresses')
        ->onDelete('cascade');
        $table->primary(['user_id', 'address_id']);

        $table->timestamps();
    });

And order migrate before address.

3
  • 1
    where is the migration for 'addresses'? Commented Jul 17, 2020 at 21:52
  • The problem is on your addreses table, may be you created it later (before migrate the the orders table). Or your primary key incorrectly formatted Commented Jul 18, 2020 at 5:18
  • @lagbox question updated Commented Jul 18, 2020 at 10:17

2 Answers 2

3

You must first create the table, then create the foreign keys:

Schema::create('orders', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id');
        $table->unsignedBigInteger('address_id');
        $table->bigInteger('price');
        $table->string('post_type');
        $table->enum('status',['unpaid','paid','preparation','posted', 'recieved', 'canceled']);
        $table->string('tracking_serial')->nullable();
        $table->primary(['user_id', 'address_id']);
        $table->timestamps();
    });


Schema::table('orders', function (Blueprint $table) {

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade');
    });
Sign up to request clarification or add additional context in comments.

Comments

2

i hope you have used a migration to change the order table and not just modified the original migration?

this error generally happens for few reasons:

  1. the fk field (address_id) and pk field (i.e. address) are not the same type
  2. the migration for order is running BEFORE address table (i think unlikely in this case as error would be different)
  3. address_id is not nullable (from what i can see) therefore when u create a FK the rows that exist currently wont have a valid FK to address. (so make it nullable)

3 Comments

yes my order table run befor address table, what can i do
i run address table before order table and my problem solved. thanks
@Waltun just follow up - you can just change the timestamp of address migration to be before the order table..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.