0

Im trying to run php artisan migrate but i keep getting the following error:

i have tried to roll back the tables and migrate them again but it just gives me the same error

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `comments` add constraint `comments_post_id_foreign` foreign key (`post_id`) references `posts` (`id`) on delete cascade)

Here is my create_comments_table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommentsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->id();
        $table->mediumText('body');
        $table->string('email');
        $table->string('name');
        $table->boolean('approved');
        $table->integer('post_id')->unsigned();
        $table->timestamps();

    });

    Schema::table('comments', function ($table) {
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
    });

}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropforeign(['post_id']);
    Schema::dropIfExists('comments');
}
}

Here is my create_posts_table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->mediumText('body');
        $table->string('name');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('posts');
}
}

And my migration tables are being created in the following order:

create users table
create passwords 
create failed jobs
create posts 
create comments

How can i fix this?

es915
  • 137
  • 3
  • 11
  • Does this answer your question? [MySQL Error 1215: Cannot add foreign key constraint](https://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint) – Dan Dec 18 '20 at 14:53
  • It is because the id of the post table is not integer, or the post table is not yet created – MrEduar Dec 18 '20 at 15:03

1 Answers1

0
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommentsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->id();
        $table->mediumText('body');
        $table->string('email');
        $table->string('name');
        $table->boolean('approved');
        $table->integer('post_id')->unsigned();
        $table->timestamps();
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('comments');
}
}

Try to update the comment migrate the file like this

HEL Mab
  • 35
  • 1
  • 4
  • Yes sorry i forgot to change that above. Even when i change it to posts it still gives me the error – es915 Dec 18 '20 at 15:58
  • Please try to update the comment migration file like what's I mention – HEL Mab Dec 18 '20 at 16:09
  • And make sure the posts table need to create before the comments table – HEL Mab Dec 18 '20 at 16:12
  • when i run php artisan migrate i think only my create_comments_table if migrating as it starts with this table and if i delete the table and run the command again it says nothing to migrate even when i rollback. could this be the problem? – es915 Dec 18 '20 at 16:12
  • Yes, it was the problem, :D but if you update what's I mentioned above, and try to run ```php artisan migrate:fresh```, I think it should work – HEL Mab Dec 18 '20 at 16:20
  • Okay great that worked thank you for the help. – es915 Dec 18 '20 at 16:36