LouLav
LouLav

Reputation: 149

Laravel : Migration with Foreign Key

I know this question has been ask a lot of time but i tried every solutions and it still give me an error.

I want to migrate my tables :

class CreateFichesTable extends Migration
{

public function up()
{
    Schema::create('fiches', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('type');
        $table->string('nom');
        $table->string('description');
        $table->string('image');
        $table->integer('equipe_id')->unsigned();
        $table->timestamps();
    });

    Schema::table('fiches', function(Blueprint $table)
    {
        $table->foreign('equipe_id')->references('id')->on('equipes');
    });
}

}

And

class CreateEquipesTable extends Migration
{

public function up()
{
    Schema::create('equipes', function (Blueprint $table) {;
        $table->increments('id');
        $table->string('nom');
        $table->string('image');
        $table->timestamps();
    });
}

}

And I get :

Exception trace:

1   PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign 
key constraint")

I tried to force using the engine "InnoDB" but still not working.

Any tip ?

Upvotes: 1

Views: 68

Answers (1)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

When you create new migration file laravel automatically added in the start datetime like this 2018_08_10_111004_ for detect which migration must be create firstly. As you show in comments your migration files is 2018_08_31_141536_create_fiches_table.php and 2018_09_03_141649_create_equipes_table.php you must be change one of this migration for firstly called second migration the first.
For example

2018_08_31_141536_create_fiches_table.php 
2018_08_30_141649_create_equipes_table.php

Upvotes: 2

Related Questions