Search code examples
phpmysqllaravelmigration

Laravel foreign key on migration is not working


I am creating a fresh application on Laravel and I am writing the migrations I want to set the foreign key for my columns so I am doing like below :

   Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->integer('type_id');
            $table->string('name');
            $table->integer('status_id')->default(0);
            $table->integer('category_id')->default(0);
            $table->integer('store_id');
            $table->timestamps();
            $table->foreign('status_id')->references('id')->on('product_statuses');
            $table->index('status_id');
            $table->foreign('type_id')->references('id')->on('product_types');
            $table->index('type_id');
            $table->foreign('category_id')->references('id')->on('product_categories');
            $table->index('category_id');
            $table->foreign('store_id')->references('id')->on('stores');
            $table->index('store_id');

but these are not working When I check it in phpmyadmin it lets me insert any number, not the item from status_id for example, and when I check it in the design tab I don't see the relation between the tables. #EDIT

adding the product_types migration :

 /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product_types', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

and about the engine, I am using Wamp with MySQL v8 which I think sups the fk feature


Solution

  • As you stated in the comments:

    what i see is that in phpmyadmin on tables there is a column which is writing :Type :MyISAM . is that the same meaning of engine ?

    Your DB default engine is MyISAM which does not support relational features.

    To fix that you can edit your config/database.php file, search for mysql entry and change:

    'engine' => null,
    

    to

    'engine' => 'InnoDB',
    

    Then you'll have to recreate the tables.


    If you can't drop and recreate the tables for any reason, you can create a new migration to alter the existing tables. Ie:

    public function up()
    {
        $tables = [
            'product_types',
            'products',
        ];
        foreach ($tables as $table) {
            DB::statement('ALTER TABLE ' . $table . ' ENGINE = InnoDB');
        }
    }
    

    Another thing, it's the data type of the foreign keys columns must match the same data type of the related column.

    Since $table->id() is an alias of $table->bigIncrements('id') as stated in laravel latest versión docs, you should use:

    $table->unsignedBigInteger('type_id');
    
    $table->foreign('type_id')->references('id')->on('product_types');
    

    Also note the order: create the column first, and the the fk reference (and not the inverse).

    Reference: https://laravel.com/docs/8.x/migrations#foreign-key-constraints