I am having trouble adding a foreign key
to laravel 5.4. Here is the code for migrations
below:
Schema::create('anexos_posts', function (Blueprint $table) {
$table->increments('id');
$table->string('anexo');
$table->integer('post_id')->unsigned();
$table->timestamps();
});
Then another migration
is executed by adding foreign
:
Schema::table('anexos_posts', function (Blueprint $table) {
$table->foreign('post_id')->references('id')->on('postagens');
});
Error:
Schema :: table ('attachments_posts', function (Blueprint $ table) { $ table-> foreign ('post_id') -> references ('id') -> on ('posts'); }); In Connection.php line 647: SQLSTATE [HY000]: General error: 1215 Can not add
foreign key
constraint (SQL: alter tableanexos_posts
add constraintanexos_posts_post_id_foreign
foreign key (post_id
) referencespostagens
(id
))In Connection.php line 449: SQLSTATE [HY000]: General error: 1215 Can not add foreign key constraint