0

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

xshadow
  • 1
  • 1
  • 2
    `$table->integer('brand_id')->unsigned()` in the *'products'* migration creates an [`UNSIGNED INT`](https://laravel.com/docs/9.x/migrations#column-method-integer). Yet `$table->id();` in the *'brands'* migration creates an [`UNSIGNED BIGINT`](https://laravel.com/docs/9.x/migrations#column-method-id), leading to a **mismatch** in data types. – steven7mwesigwa Nov 14 '22 at 23:53
  • 1
    Use `$table->unsignedBigInteger('brand_id');` instead in the *'products'* migration. – steven7mwesigwa Nov 14 '22 at 23:58
  • Smells like pivot table which should be named "product_brand" at least and then referenced like $table->foreignId('brand_id')->constrained()->onDelete('cascade') which creates proper id. The error says not too much, the order of migrations might be broken as well. – Elboyler Nov 15 '22 at 00:02
  • [Laravel 8 Migration "General error: 1215 Cannot add foreign key constraint"](https://stackoverflow.com/questions/66736682/laravel-8-migration-general-error-1215-cannot-add-foreign-key-constraint) , [Migration: Cannot add foreign key constraint](https://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint) – steven7mwesigwa Nov 15 '22 at 00:04
  • [General error: 1215 Cannot add foreign key constraint, Laravel 5 & MySQL](https://stackoverflow.com/questions/55745137/general-error-1215-cannot-add-foreign-key-constraint-laravel-5-mysql) – steven7mwesigwa Nov 15 '22 at 00:09

0 Answers0