Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen_mm's avatar

Failed to create php artisan migrate

I want to create invoice application and the ERD for this application shown below : 1.) Customer table - invoice table: 1 to many , 2.) Invoice - Customer : 1 to 1 , 3.) Invoices - Product = many to many.

And our file migrations show below : 1.) Customer migrations

public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('name')->nullable();
            $table->string('email')->nullable();
            $table->string('address')->nullable();
            $table->bigInteger('phone_number')->nullable();
            $table->timestamps();
        });
    }

2.) Producs Migrations

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('description')->nullable();
            $table->string('item_type')->nullable();
            $table->bigInteger('unit_price')->nullable();
            $table->bigInteger('amount')->nullable();
            $table->timestamps();
        });
    }

3.) Invoices migrations

public function up()
    {
        Schema::create('invoices', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('customer_id');
            $table->string('subject')->nullable();
            $table->bigInteger('subtotal')->nullable();
            $table->bigInteger('tax')->nullable();
            $table->bigInteger('payment')->nullable();
            $table->bigInteger('amount_date')->nullable();
            $table->timestamp('issue_date')->nullable();
            $table->timestamp('due_date')->nullable();
            $table->timestamps();
        });
    } 

4.) Invoices_products Migrations

public function up()
    {
        Schema::create('invoices_products', function (Blueprint $table) {
            $table->id();
            $table->integer('invoice_id')->unsigned()->index();
            $table->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade');
            $table->integer('product_id')->unsigned()->index();
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->primary(['invoice_id', 'product_id']);
            $table->string('subject')->nullable();
            $table->bigInteger('price')->nullable();
            $table->bigInteger('quantity')->nullable();
        });
    }

But when i run 'php artisan migrate', I got this error :

SQLSTATE[HY000]: General error: 1005 Can't create table `invoice`.`invoices_products` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `invoi
ces_products` add constraint `invoices_products_invoice_id_foreign` foreign key (`invoice_id`) references `invoices` (`id`) on delete cascade)

Do you have any solutions for this problem? Thank you

0 likes
14 replies
Sinnbeck's avatar

Where is the migration to create invoices_products? Maybe you meant to do

Schema::create('invoices_products', function (Blueprint $table)
stephen_mm's avatar

@Sinnbeck Yes, i mean Schema::table('invoices_products''), but i got the same error message

stephen_mm's avatar

@AungHtetPaing__ Pardon, this is my first create thread in laracast forum. Do you mean i have to format all of my migrations table?

Sinnbeck's avatar

@stephen_mm To format code here you simply add ``` on the line just before and after your code (on a line by itself) :)

1 like
AungHtetPaing__'s avatar

@stephen_mm I mean you should format all of code which include in your question. Currently you are only formatting the last 7 lines of code. Not related with your error.


			```
				code
			```

AungHtetPaing__'s avatar

@stephen_mm

public function up()
    {
        Schema::create('invoices_products', function (Blueprint $table) {
            $table->id();
			$table->foreignId('invoice_id')->constrained();
			$table->foreignId('invoice_id')->constrained();
            $table->string('subject')->nullable();
            $table->bigInteger('price')->nullable();
            $table->bigInteger('quantity')->nullable();
        });
    }
1 like
stephen_mm's avatar

@AungHtetPaing__ Did you mean like this Sir?, Why you write double invoice_id?

			$table->foreignId('invoice_id')->constrained();
			$table->foreignId('product_id')->constrained();

Please or to participate in this conversation.