SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table products
add constraint products_brand_id_foreign
foreign key (brand_id
) references product_brands
(id
) on delete cascade)
This is my brands migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductBrandsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_brands', function (Blueprint $table) {
$table->id();
$table->string('brand_name');
$table->string('brand_status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_brands');
}
}
and this is my products migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_name');
$table->string('sku');
$table->string('product_unit_price');
$table->string('quantity');
$table->string('product_image');
$table->string('product_description');
$table->integer('brand_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->integer('unit_id')->unsigned();
$table->string('vat_type');
$table->timestamps();
});
Schema::table('products', function (Blueprint $table) {
$table->foreign('brand_id')->references('id')->on('product_brands')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('product_categories')->onDelete('cascade');
$table->foreign('unit_id')->references('id')->on('units')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}