Unable to apply constraints/foreign in Laravel 5.3 Migrations

  eloquent, laravel, laravel-5, orm, php

I’m contacting you because I absolutely can’t find the solution after having spent a lot of time on it…

Here is my problem:

I naturally created my tables on Laravel 5.3 (resumption of a project) for the arrival of a new feature.

After setting up everything, Model/ Controller/CRUD I noticed a problem with the ORM.
When I go to create the table with its constraints, foreign keys through the orm. It doesn’t apply it fully. That is, it will define the key as an index but it will not create the constraint…
I tried to use a DB::statement but it gives the same result.

I tried to update doctrine/dblab but it didn’t work.
Do you think the MySQL version could be the cause?
Something wrong with the settings?
Knowing that the previous migrations the constraints are present!

Thanks in advance to those who will help me!

Application Types :

Schema::create('application_types', function (Blueprint $table) {
           
            //Ids
            $table->increments('id');

            // details
            $table->char('type', 50)->unique();

            // dates
            $table->timestamp('created_at')->useCurrent(); // useCurrent equals to -> default(DB::raw('CURRENT_TIMESTAMP'))
            $table->timestamp('updated_at')->nullable()->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'));
        });

Aplication Categories :

Schema::create('application_categories', function (Blueprint $table) {
           
            //Ids
            $table->increments('id');

            // details
            $table->char('name', 50)->unique();

            // dates
            $table->timestamp('created_at')->useCurrent(); // useCurrent equals to -> default(DB::raw('CURRENT_TIMESTAMP'))
            $table->timestamp('updated_at')->nullable()->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'));
        });

Applications :

Schema::create('applications', function (Blueprint $table) {
           
            //Ids
            $table->increments('id', true);
            $table->integer('type_id')->unsigned(); // -> link to application types table
            $table->integer('document_id')->unsigned(); // -> link to documents table
            $table->integer('category_id')->unsigned(); // -> link to application category table
            $table->integer('referent_id')->unsigned(); // -> link to dir_people table
            $table->integer('created_by_user_id')->unsigned(); // -> link to users table
            $table->integer('updated_by_user_id')->unsigned()->nullable(); // -> link to users table


            // details
            $table->char('name', 50);
            $table->longtext('description');
            $table->longtext('path');
            $table->boolean('is_active')->default(1);

            // dates
            $table->timestamp('created_at')->useCurrent(); // useCurrent equals to -> default(DB::raw('CURRENT_TIMESTAMP'))
            $table->timestamp('updated_at')->nullable()->default(DB::raw('null ON UPDATE CURRENT_TIMESTAMP'));      
      });

// == Set foreign keys == 
Schema::table('applications', function (Blueprint $table) {
            
            //Document
            $table->unique('document_id');

            $table->foreign('document_id')
                  ->references('id')->on('documents')
                  ->onDelete('cascade');

            // Application type
            $table->foreign('type_id')
                  ->references('id')->on('application_types');

            // Application Category
            $table->foreign('category_id')
            ->references('id')->on('application_categories');

            // Referent_id
            $table->foreign('referent_id')
                  ->references('id')->on('dir_people');
            
            // Created by User
            $table->foreign('created_by_user_id')
                  ->references('id')->on('users');
            
            // Updated by User
            $table->foreign('updated_by_user_id')
                  ->references('id')->on('users');
      });      

Img : Viewpoint on HeidiSQL

Source: Laravel

Leave a Reply