Pedro
Pedro

Reputation: 1477

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

Upvotes: 0

Views: 224

Answers (1)

Ali Rasheed
Ali Rasheed

Reputation: 2815

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

Upvotes: 1

Related Questions