Search code examples
laravellaravel-5laravel-5.3database-migration

Cant migrate tables with foreign keys


im creating some migrations in my app but is giving a error of:

"General error: 1005 Can't create table+ "Foreign key constraint is incorrectly formed")+laravel".

Cant find the problem with my migrations.

Schema::create('articles', function (Blueprint $table) {
    $table->increments('id');
    $table->boolean('online');
    $table->timestamps();
});

Schema::create('article_translations', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('article_id')->unsigned();
    $table->string('locale')->index();

    $table->string('name');
    $table->text('text');

    $table->unique(['article_id','locale']);
    $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
});

Solution

  • Why don't you try it separately. like this

    Schema::table('article_translations', function($table) 
    {     
        $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
    });