简体   繁体   中英

how to fix foreign key error in laravel migration

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.

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 (ie 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)

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');
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM