Home > front end >  Unable to apply constraints/foreign in Laravel 5.3 Migrations
Unable to apply constraints/foreign in Laravel 5.3 Migrations

Time:11-26

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!

Look : Img : Viewpoint on HeidiSQL

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

CodePudding user response:

The foreign keys are not created in the database because, it must reference a primary key, witch i don't think its the case on your migrations

try adding in your tables:

$table->primary('id');

CodePudding user response:

[Author of the post]

I checked if I could create it directly on MySql and what was my surprise to see that it did not work.

So the code on the ORM side is good!

However I found the solution, it could help someone.

By switching to InnoDB as an engine it worked

If you use WAMP check that the installation is well done ! (mine was)

Then add to app/config/database :

'connections' => [
   'mysql' => [
      //...
      'engine' => 'InnoDB'
   ]
]

else

In your migrations files :

$table->engine = 'InnoDB';

It is absolutely necessary that all the tables are created with InnoDB otherwise it does not work !

  • Related