Where is the migration to create invoices_products? Maybe you meant to do
Schema::create('invoices_products', function (Blueprint $table)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
Please or to participate in this conversation.